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 | /* * Copyright (c) 2000-2020 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ /* * Copyright (c) 1982, 1986, 1989, 1993 * The Regents of the University of California. All rights reserved. * (c) UNIX System Laboratories, Inc. * All or some portions of this file are derived from material licensed * to the University of California by American Telephone and Telegraph * Co. or Unix System Laboratories, Inc. and are reproduced herein with * the permission of UNIX System Laboratories, Inc. * * 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. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. 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 BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE 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. * * @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95 * */ /* * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce * support for mandatory and extensible security protections. This notice * is included in support of clause 2.2 (b) of the Apple Public License, * Version 2.0. */ #include <sys/param.h> #include <sys/types.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/file_internal.h> #include <sys/stat.h> #include <sys/proc_internal.h> #include <sys/kauth.h> #include <sys/mount_internal.h> #include <sys/namei.h> #include <sys/vnode_internal.h> #include <sys/ioctl.h> #include <sys/fsctl.h> #include <sys/tty.h> #include <sys/ubc.h> #include <sys/conf.h> #include <sys/disk.h> #include <sys/fsevents.h> #include <sys/kdebug.h> #include <sys/xattr.h> #include <sys/ubc_internal.h> #include <sys/uio_internal.h> #include <sys/resourcevar.h> #include <sys/signalvar.h> #include <vm/vm_kern.h> #include <vm/vm_map.h> #include <miscfs/specfs/specdev.h> #include <miscfs/fifofs/fifo.h> #if CONFIG_MACF #include <security/mac_framework.h> #endif #include <IOKit/IOBSD.h> #include <libkern/section_keywords.h> static int vn_closefile(struct fileglob *fp, vfs_context_t ctx); static int vn_ioctl(struct fileproc *fp, u_long com, caddr_t data, vfs_context_t ctx); static int vn_read(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx); static int vn_write(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx); static int vn_select( struct fileproc *fp, int which, void * wql, vfs_context_t ctx); static int vn_kqfilter(struct fileproc *fp, struct knote *kn, struct kevent_qos_s *kev); static void filt_vndetach(struct knote *kn); static int filt_vnode(struct knote *kn, long hint); static int filt_vnode_common(struct knote *kn, struct kevent_qos_s *kev, vnode_t vp, long hint); static int vn_open_auth_finish(vnode_t vp, int fmode, vfs_context_t ctx); const struct fileops vnops = { .fo_type = DTYPE_VNODE, .fo_read = vn_read, .fo_write = vn_write, .fo_ioctl = vn_ioctl, .fo_select = vn_select, .fo_close = vn_closefile, .fo_drain = fo_no_drain, .fo_kqfilter = vn_kqfilter, }; static int filt_vntouch(struct knote *kn, struct kevent_qos_s *kev); static int filt_vnprocess(struct knote *kn, struct kevent_qos_s*kev); SECURITY_READ_ONLY_EARLY(struct filterops) vnode_filtops = { .f_isfd = 1, .f_attach = NULL, .f_detach = filt_vndetach, .f_event = filt_vnode, .f_touch = filt_vntouch, .f_process = filt_vnprocess, }; /* * Common code for vnode open operations. * Check permissions, and call the VNOP_OPEN or VNOP_CREATE routine. * * XXX the profusion of interfaces here is probably a bad thing. */ int vn_open(struct nameidata *ndp, int fmode, int cmode) { return vn_open_modflags(ndp, &fmode, cmode); } int vn_open_modflags(struct nameidata *ndp, int *fmodep, int cmode) { int error; struct vnode_attr *vap; vap = kalloc_type(struct vnode_attr, Z_WAITOK); VATTR_INIT(vap); VATTR_SET(vap, va_mode, (mode_t)cmode); error = vn_open_auth(ndp, fmodep, vap, NULLVP); kfree_type(struct vnode_attr, vap); return error; } static int vn_open_auth_finish(vnode_t vp, int fmode, vfs_context_t ctx) { int error; if ((error = vnode_ref_ext(vp, fmode, 0)) != 0) { goto bad; } /* Call out to allow 3rd party notification of open. * Ignore result of kauth_authorize_fileop call. */ #if CONFIG_MACF mac_vnode_notify_open(ctx, vp, fmode); #endif kauth_authorize_fileop(vfs_context_ucred(ctx), KAUTH_FILEOP_OPEN, (uintptr_t)vp, 0); return 0; bad: return error; } /* * May do nameidone() to allow safely adding an FSEvent. Cue off of ni_dvp to * determine whether that has happened. */ static int vn_open_auth_do_create(struct nameidata *ndp, struct vnode_attr *vap, int fmode, boolean_t *did_create, boolean_t *did_open, vfs_context_t ctx) { uint32_t status = 0; vnode_t dvp = ndp->ni_dvp; int batched; int error; vnode_t vp; batched = vnode_compound_open_available(ndp->ni_dvp); *did_open = FALSE; VATTR_SET(vap, va_type, VREG); if (fmode & O_EXCL) { vap->va_vaflags |= VA_EXCLUSIVE; } #if NAMEDRSRCFORK if (ndp->ni_cnd.cn_flags & CN_WANTSRSRCFORK) { /* Resource fork creation - check additional read permissions if FREAD is set */ if (fmode & FREAD) { #if CONFIG_MACF /* Need MAC getextattr check for read access */ error = mac_vnode_check_getextattr(ctx, dvp, XATTR_RESOURCEFORK_NAME, NULL); if (error) { goto out; } #endif /* CONFIG_MACF */ /* Need read extended attribute permission for read access */ if ((error = vnode_authorize(dvp, NULL, KAUTH_VNODE_READ_EXTATTRIBUTES, ctx)) != 0) { goto out; } } /* * vn_authorize_create will unconditionally check POSIX/MAC write permissions * (regardless of FWRITE) because we are creating the file. */ if ((error = vn_authorize_create(dvp, &ndp->ni_cnd, vap, ctx, NULL)) != 0) { goto out; } if ((error = vnode_makenamedstream(dvp, &ndp->ni_vp, XATTR_RESOURCEFORK_NAME, 0, ctx)) != 0) { goto out; } *did_create = TRUE; } else { #endif if (!batched) { if ((error = vn_authorize_create(dvp, &ndp->ni_cnd, vap, ctx, NULL)) != 0) { goto out; } } error = vn_create(dvp, &ndp->ni_vp, ndp, vap, VN_CREATE_DOOPEN, fmode, &status, ctx); if (error != 0) { if (batched) { *did_create = (status & COMPOUND_OPEN_STATUS_DID_CREATE) ? TRUE : FALSE; } else { *did_create = FALSE; } if (error == EKEEPLOOKING) { if (*did_create) { panic("EKEEPLOOKING, but we did a create?"); } if (!batched) { panic("EKEEPLOOKING from filesystem that doesn't support compound vnops?"); } if ((ndp->ni_flag & NAMEI_CONTLOOKUP) == 0) { panic("EKEEPLOOKING, but continue flag not set?"); } /* * Do NOT drop the dvp: we need everything to continue the lookup. */ return error; } } else { if (batched) { *did_create = (status & COMPOUND_OPEN_STATUS_DID_CREATE) ? 1 : 0; *did_open = TRUE; } else { *did_create = TRUE; } } #if NAMEDRSRCFORK } #endif vp = ndp->ni_vp; if (*did_create) { int update_flags = 0; // Make sure the name & parent pointers are hooked up if (vp->v_name == NULL) { update_flags |= VNODE_UPDATE_NAME; } if (vp->v_parent == NULLVP) { update_flags |= VNODE_UPDATE_PARENT; } if (update_flags) { vnode_update_identity(vp, dvp, ndp->ni_cnd.cn_nameptr, ndp->ni_cnd.cn_namelen, ndp->ni_cnd.cn_hash, update_flags); } vnode_put(dvp); ndp->ni_dvp = NULLVP; #if CONFIG_FSE if (need_fsevent(FSE_CREATE_FILE, vp)) { add_fsevent(FSE_CREATE_FILE, ctx, FSE_ARG_VNODE, vp, FSE_ARG_DONE); } #endif } out: if (ndp->ni_dvp != NULLVP) { vnode_put(dvp); ndp->ni_dvp = NULLVP; } return error; } /* * This is the number of times we'll loop in vn_open_auth without explicitly * yielding the CPU when we determine we have to retry. */ #define RETRY_NO_YIELD_COUNT 5 /* * Open a file with authorization, updating the contents of the structures * pointed to by ndp, fmodep, and vap as necessary to perform the requested * operation. This function is used for both opens of existing files, and * creation of new files. * * Parameters: ndp The nami data pointer describing the * file * fmodep A pointer to an int containg the mode * information to be used for the open * vap A pointer to the vnode attribute * descriptor to be used for the open * authvp If non-null and VA_DP_AUTHENTICATE is set in vap, * have a supporting filesystem verify that the file * to be opened is on the same volume as authvp and * that authvp is on an authenticated volume * * Indirect: * Contents of the data structures pointed * to by the parameters are modified as * necessary to the requested operation. * * Returns: 0 Success * !0 errno value * * Notes: The kauth_filesec_t in 'vap', if any, is in host byte order. * * The contents of '*ndp' will be modified, based on the other * arguments to this function, and to return file and directory * data necessary to satisfy the requested operation. * * If the file does not exist and we are creating it, then the * O_TRUNC flag will be cleared in '*fmodep' to indicate to the * caller that the file was not truncated. * * If the file exists and the O_EXCL flag was not specified, then * the O_CREAT flag will be cleared in '*fmodep' to indicate to * the caller that the existing file was merely opened rather * than created. * * The contents of '*vap' will be modified as necessary to * complete the operation, including setting of supported * attribute, clearing of fields containing unsupported attributes * in the request, if the request proceeds without them, etc.. * * XXX: This function is too complicated in actings on its arguments * * XXX: We should enummerate the possible errno values here, and where * in the code they originated. */ int vn_open_auth(struct nameidata *ndp, int *fmodep, struct vnode_attr *vap, vnode_t authvp) { struct vnode *vp; struct vnode *dvp; vfs_context_t ctx = ndp->ni_cnd.cn_context; int error; int fmode; uint32_t origcnflags; boolean_t did_create; boolean_t did_open; boolean_t need_vnop_open; boolean_t batched; boolean_t ref_failed; int nretries = 0, max_retries = 10; again: vp = NULL; dvp = NULL; batched = FALSE; did_create = FALSE; need_vnop_open = TRUE; ref_failed = FALSE; fmode = *fmodep; origcnflags = ndp->ni_cnd.cn_flags; if (VATTR_IS_ACTIVE(vap, va_dataprotect_flags)) { if ((authvp != NULLVP) && !ISSET(vap->va_dataprotect_flags, VA_DP_AUTHENTICATE)) { error = EINVAL; goto out; } // If raw encrypted mode is requested, handle that here if (ISSET(vap->va_dataprotect_flags, VA_DP_RAWENCRYPTED)) { fmode |= FENCRYPTED; } } if ((fmode & O_NOFOLLOW_ANY) && (fmode & O_NOFOLLOW)) { error = EINVAL; goto out; } /* * We only call nameidone when exiting the function because we may * end up needing the pathname buffer in the component structure. */ /* * O_CREAT */ if (fmode & O_CREAT) { if ((fmode & O_DIRECTORY)) { error = EINVAL; goto out; } ndp->ni_cnd.cn_nameiop = CREATE; #if CONFIG_TRIGGERS ndp->ni_op = OP_LINK; #endif /* Inherit USEDVP, vnode_open() supported flags only */ ndp->ni_cnd.cn_flags &= (USEDVP | NOCROSSMOUNT); ndp->ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF | AUDITVNPATH1; /* Inherit NAMEI_ROOTDIR flag only */ ndp->ni_flag &= NAMEI_ROOTDIR | NAMEI_ATFD; ndp->ni_flag |= NAMEI_COMPOUNDOPEN; #if NAMEDRSRCFORK /* open calls are allowed for resource forks. */ ndp->ni_cnd.cn_flags |= CN_ALLOWRSRCFORK; #endif if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0 && (origcnflags & FOLLOW) != 0 && (fmode & O_SYMLINK) == 0) { ndp->ni_cnd.cn_flags |= FOLLOW; } if (fmode & O_NOFOLLOW_ANY) { /* will return ELOOP on the first symlink to be hit */ ndp->ni_flag |= NAMEI_NOFOLLOW_ANY; } if (fmode & O_RESOLVE_BENEATH) { /* will return ENOTCAPABLE if path resolution escapes the starting directory */ ndp->ni_flag |= NAMEI_RESOLVE_BENEATH; } if (fmode & O_UNIQUE) { /* will return ENOTCAPABLE if target vnode has multiple links */ ndp->ni_flag |= NAMEI_UNIQUE; } continue_create_lookup: if ((error = namei(ndp))) { goto out; } dvp = ndp->ni_dvp; vp = ndp->ni_vp; batched = vnode_compound_open_available(dvp); /* not found, create */ if (vp == NULL) { /* must have attributes for a new file */ if (vap == NULL) { vnode_put(dvp); error = EINVAL; goto out; } /* * Attempt a create. For a system supporting compound VNOPs, we may * find an existing file or create one; in either case, we will already * have the file open and no VNOP_OPEN() will be needed. */ error = vn_open_auth_do_create(ndp, vap, fmode, &did_create, &did_open, ctx); dvp = ndp->ni_dvp; vp = ndp->ni_vp; /* Detected a node that the filesystem couldn't handle */ if (error == EKEEPLOOKING) { if (!batched) { panic("EKEEPLOOKING from a filesystem that doesn't support compound VNOPs?"); } goto continue_create_lookup; } if (dvp) { panic("Shouldn't have a dvp here."); } if (error) { /* * Check for a create race. */ if ((error == EEXIST) && !(fmode & O_EXCL)) { if (vp) { vnode_put(vp); } nameidone(ndp); goto again; } goto bad; } need_vnop_open = !did_open; } else { if (fmode & O_EXCL) { error = EEXIST; } /* * We have a vnode. Use compound open if available * or else fall through to "traditional" path. Note: can't * do a compound open for root, because the parent belongs * to a different FS. */ if (error == 0 && batched && (vnode_mount(dvp) == vnode_mount(vp))) { error = VNOP_COMPOUND_OPEN(dvp, &ndp->ni_vp, ndp, 0, fmode, NULL, NULL, ctx); if (error == 0) { vp = ndp->ni_vp; need_vnop_open = FALSE; } else if (error == EKEEPLOOKING) { if ((ndp->ni_flag & NAMEI_CONTLOOKUP) == 0) { panic("EKEEPLOOKING, but continue flag not set?"); } goto continue_create_lookup; } } vnode_put(dvp); ndp->ni_dvp = NULLVP; if (error) { goto bad; } fmode &= ~O_CREAT; /* Fall through */ } } else { /* * Not O_CREAT */ ndp->ni_cnd.cn_nameiop = LOOKUP; /* Inherit USEDVP, vnode_open() supported flags only */ ndp->ni_cnd.cn_flags &= (USEDVP | NOCROSSMOUNT); ndp->ni_cnd.cn_flags |= FOLLOW | LOCKLEAF | AUDITVNPATH1 | WANTPARENT; #if NAMEDRSRCFORK /* open calls are allowed for resource forks. */ ndp->ni_cnd.cn_flags |= CN_ALLOWRSRCFORK; #endif if (fmode & FENCRYPTED) { ndp->ni_cnd.cn_flags |= CN_RAW_ENCRYPTED | CN_SKIPNAMECACHE; } /* Inherit NAMEI_ROOTDIR flag only */ ndp->ni_flag &= NAMEI_ROOTDIR | NAMEI_ATFD; ndp->ni_flag |= NAMEI_COMPOUNDOPEN; /* preserve NOFOLLOW from vnode_open() */ if (fmode & O_NOFOLLOW || fmode & O_SYMLINK || (origcnflags & FOLLOW) == 0) { ndp->ni_cnd.cn_flags &= ~FOLLOW; } if (fmode & O_NOFOLLOW_ANY) { /* will return ELOOP on the first symlink to be hit */ ndp->ni_flag |= NAMEI_NOFOLLOW_ANY; } if (fmode & O_RESOLVE_BENEATH) { /* will return ENOTCAPABLE if path resolution escapes the starting directory */ ndp->ni_flag |= NAMEI_RESOLVE_BENEATH; } if (fmode & O_UNIQUE) { /* will return ENOTCAPABLE if target vnode has multiple links */ ndp->ni_flag |= NAMEI_UNIQUE; } /* Do a lookup, possibly going directly to filesystem for compound operation */ do { if ((error = namei(ndp))) { goto out; } vp = ndp->ni_vp; dvp = ndp->ni_dvp; /* Check for batched lookup-open */ batched = vnode_compound_open_available(dvp); if (batched && ((vp == NULLVP) || (vnode_mount(dvp) == vnode_mount(vp)))) { error = VNOP_COMPOUND_OPEN(dvp, &ndp->ni_vp, ndp, 0, fmode, NULL, NULL, ctx); vp = ndp->ni_vp; if (error == 0) { need_vnop_open = FALSE; } else if (error == EKEEPLOOKING) { if ((ndp->ni_flag & NAMEI_CONTLOOKUP) == 0) { panic("EKEEPLOOKING, but continue flag not set?"); } } else if ((fmode & (FREAD | FWRITE | FEXEC)) == FEXEC) { /* * Some file systems fail in vnop_open call with absense of * both FREAD and FWRITE access modes. Retry the vnop_open * call again with FREAD access mode added. */ error = VNOP_COMPOUND_OPEN(dvp, &ndp->ni_vp, ndp, 0, fmode | FREAD, NULL, NULL, ctx); if (error == 0) { vp = ndp->ni_vp; need_vnop_open = FALSE; } } } } while (error == EKEEPLOOKING); vnode_put(dvp); ndp->ni_dvp = NULLVP; if (error) { goto bad; } } /* * By this point, dvp iocount is dropped and dvp pointer is cleared. */ if (ndp->ni_dvp != NULLVP) { panic("Haven't cleaned up adequately in vn_open_auth()"); } /* Verify that the vnode returned from namei() has the expected fileid and fsid */ if (VATTR_IS_ACTIVE(vap, va_flags) && ISSET(vap->va_flags, VA_VAFILEID)) { vnode_t tmp_vp; if (!VATTR_IS_ACTIVE(vap, va_fsid64) || !VATTR_IS_ACTIVE(vap, va_fileid)) { error = EINVAL; goto bad; } error = vnode_getfromid(vap->va_fsid64.val[0], vap->va_fileid, ctx, FSOPT_ISREALFSID, &tmp_vp); if (error) { goto bad; } if (tmp_vp != vp) { vnode_put(tmp_vp); error = ERECYCLE; goto bad; } vnode_put(tmp_vp); } /* * Expect to use this code for filesystems without compound VNOPs, for the root * of a filesystem, which can't be "looked up" in the sense of VNOP_LOOKUP(), * and for shadow files, which do not live on the same filesystems as their "parents." */ if (need_vnop_open) { if (batched && !vnode_isvroot(vp) && !vnode_isnamedstream(vp)) { panic("Why am I trying to use VNOP_OPEN() on anything other than the root or a named stream?"); } if (!did_create) { error = vn_authorize_open_existing(vp, &ndp->ni_cnd, fmode, ctx, NULL); if (error) { goto bad; } } if (VATTR_IS_ACTIVE(vap, va_dataprotect_flags)) { if (ISSET(vap->va_dataprotect_flags, VA_DP_RAWUNENCRYPTED)) { /* Don't allow unencrypted io request from user space unless entitled */ boolean_t entitled = FALSE; #if !SECURE_KERNEL entitled = IOCurrentTaskHasEntitlement("com.apple.private.security.file-unencrypt-access"); #endif /* SECURE_KERNEL */ if (!entitled) { error = EPERM; goto bad; } fmode |= FUNENCRYPTED; } if (ISSET(vap->va_dataprotect_flags, VA_DP_AUTHENTICATE)) { fsioc_auth_fs_t afs = { .authvp = authvp }; error = VNOP_IOCTL(vp, FSIOC_AUTH_FS, (caddr_t)&afs, 0, ctx); if (error) { goto bad; } } } error = VNOP_OPEN(vp, fmode, ctx); if (error) { /* * Some file systems fail in vnop_open call with absense of both * FREAD and FWRITE access modes. Retry the vnop_open call again * with FREAD access mode added. */ if ((fmode & (FREAD | FWRITE | FEXEC)) == FEXEC) { error = VNOP_OPEN(vp, fmode | FREAD, ctx); } if (error) { goto bad; } } need_vnop_open = FALSE; } // if the vnode is tagged VOPENEVT and the current process // has the P_CHECKOPENEVT flag set, then we or in the O_EVTONLY // flag to the open mode so that this open won't count against // the vnode when carbon delete() does a vnode_isinuse() to see // if a file is currently in use. this allows spotlight // importers to not interfere with carbon apps that depend on // the no-delete-if-busy semantics of carbon delete(). // if (!did_create && (vp->v_flag & VOPENEVT) && (current_proc()->p_flag & P_CHECKOPENEVT)) { fmode |= O_EVTONLY; } /* * Grab reference, etc. */ error = vn_open_auth_finish(vp, fmode, ctx); if (error) { ref_failed = TRUE; goto bad; } /* Compound VNOP open is responsible for doing the truncate */ if (batched || did_create) { fmode &= ~O_TRUNC; } *fmodep = fmode; nameidone(ndp); return 0; bad: /* Opened either explicitly or by a batched create */ if (!need_vnop_open) { VNOP_CLOSE(vp, fmode, ctx); } ndp->ni_vp = NULL; if (vp) { #if NAMEDRSRCFORK /* Aggressively recycle shadow files if we error'd out during open() */ if ((vnode_isnamedstream(vp)) && (vp->v_parent != NULLVP) && (vnode_isshadow(vp))) { vnode_recycle(vp); } #endif vnode_put(vp); /* * Check for a race against unlink. We had a vnode * but according to vnode_authorize or VNOP_OPEN it * no longer exists. * * EREDRIVEOPEN: means that we were hit by the tty allocation race or NFSv4 open/create race. */ if (((error == ENOENT) && (*fmodep & O_CREAT)) || (error == EREDRIVEOPEN) || ref_failed) { /* * We'll retry here but it may be possible that we get * into a retry "spin" inside the kernel and not allow * threads, which need to run in order for the retry * loop to end, to run. An example is an open of a * terminal which is getting revoked and we spin here * without yielding becasue namei and VNOP_OPEN are * successful but vnode_ref fails. The revoke needs * threads with an iocount to run but if spin here we * may possibly be blcoking other threads from running. * * We start yielding the CPU after some number of * retries for increasing durations. */ nretries += 1; if (nretries > max_retries) { printf("%s: reached max_retries error %d need_vnop_open %d " "fmode 0x%x ref_failed %d\n", __func__, error, need_vnop_open, *fmodep, ref_failed); goto out; } if (nretries > RETRY_NO_YIELD_COUNT) { struct timespec to; to.tv_sec = 0; to.tv_nsec = nretries * 10 * NSEC_PER_MSEC; msleep(&nretries, (lck_mtx_t *)0, PVFS, "vn_open_auth_retry", &to); } nameidone(ndp); goto again; } } out: nameidone(ndp); return error; } #if vn_access_DEPRECATED /* * Authorize an action against a vnode. This has been the canonical way to * ensure that the credential/process/etc. referenced by a vfs_context * is granted the rights called out in 'mode' against the vnode 'vp'. * * Unfortunately, the use of VREAD/VWRITE/VEXEC makes it very difficult * to add support for more rights. As such, this interface will be deprecated * and callers will use vnode_authorize instead. */ int vn_access(vnode_t vp, int mode, vfs_context_t context) { kauth_action_t action; action = 0; if (mode & VREAD) { action |= KAUTH_VNODE_READ_DATA; } if (mode & VWRITE) { action |= KAUTH_VNODE_WRITE_DATA; } if (mode & VEXEC) { action |= KAUTH_VNODE_EXECUTE; } return vnode_authorize(vp, NULL, action, context); } #endif /* vn_access_DEPRECATED */ /* * Vnode close call */ int vn_close(struct vnode *vp, int flags, vfs_context_t ctx) { int error; int flusherror = 0; #if NAMEDRSRCFORK /* Sync data from resource fork shadow file if needed. */ if ((vp->v_flag & VISNAMEDSTREAM) && (vp->v_parent != NULLVP) && vnode_isshadow(vp)) { if (flags & FWASWRITTEN) { flusherror = vnode_flushnamedstream(vp->v_parent, vp, ctx); } } #endif /* * If vnode @vp belongs to a chardev or a blkdev then it is handled * specially. We first drop its user reference count @vp->v_usecount * before calling VNOP_CLOSE(). This was done historically to ensure * that the last close of a special device vnode performed some * conditional cleanups. Now we still need to drop this reference here * to ensure that devfsspec_close() can check if the vnode is still in * use. */ if (vnode_isspec(vp)) { (void)vnode_rele_ext(vp, flags, 0); } /* * On HFS, we flush when the last writer closes. We do this * because resource fork vnodes hold a reference on data fork * vnodes and that will prevent them from getting VNOP_INACTIVE * which will delay when we flush cached data. In future, we * might find it beneficial to do this for all file systems. * Note that it's OK to access v_writecount without the lock * in this context. */ if (vp->v_tag == VT_HFS && (flags & FWRITE) && vp->v_writecount == 1) { VNOP_FSYNC(vp, MNT_NOWAIT, ctx); } error = VNOP_CLOSE(vp, flags, ctx); #if CONFIG_FSE if (flags & FWASWRITTEN) { if (need_fsevent(FSE_CONTENT_MODIFIED, vp)) { add_fsevent(FSE_CONTENT_MODIFIED, ctx, FSE_ARG_VNODE, vp, FSE_ARG_DONE); } } #endif if (!vnode_isspec(vp)) { (void)vnode_rele_ext(vp, flags, 0); } if (flusherror) { error = flusherror; } return error; } static int vn_read_swapfile( struct vnode *vp, uio_t uio) { int error; off_t swap_count, this_count; off_t file_end, read_end; off_t prev_resid; char *my_swap_page; /* * Reading from a swap file will get you zeroes. */ my_swap_page = NULL; error = 0; swap_count = uio_resid(uio); file_end = ubc_getsize(vp); read_end = uio->uio_offset + uio_resid(uio); if (uio->uio_offset >= file_end) { /* uio starts after end of file: nothing to read */ swap_count = 0; } else if (read_end > file_end) { /* uio extends beyond end of file: stop before that */ swap_count -= (read_end - file_end); } while (swap_count > 0) { if (my_swap_page == NULL) { my_swap_page = kalloc_data(PAGE_SIZE, Z_WAITOK | Z_ZERO); /* add an end-of-line to keep line counters happy */ my_swap_page[PAGE_SIZE - 1] = '\n'; } this_count = swap_count; if (this_count > PAGE_SIZE) { this_count = PAGE_SIZE; } prev_resid = uio_resid(uio); error = uiomove((caddr_t) my_swap_page, (int)this_count, uio); if (error) { break; } swap_count -= (prev_resid - uio_resid(uio)); } kfree_data(my_swap_page, PAGE_SIZE); return error; } /* * Package up an I/O request on a vnode into a uio and do it. */ int vn_rdwr( enum uio_rw rw, struct vnode *vp, caddr_t base, int len, off_t offset, enum uio_seg segflg, int ioflg, kauth_cred_t cred, int *aresid, proc_t p) { int64_t resid; int result; if (len < 0) { return EINVAL; } result = vn_rdwr_64(rw, vp, (uint64_t)(uintptr_t)base, (int64_t)len, offset, segflg, ioflg, cred, &resid, p); /* "resid" should be bounded above by "len," which is an int */ if (aresid != NULL) { *aresid = (int)resid; } return result; } int vn_rdwr_64( enum uio_rw rw, struct vnode *vp, uint64_t base, int64_t len, off_t offset, enum uio_seg segflg, int ioflg, kauth_cred_t cred, int64_t *aresid, proc_t p) { uio_t auio; int spacetype; struct vfs_context context; int error = 0; UIO_STACKBUF(uio_buf, 1); context.vc_thread = current_thread(); context.vc_ucred = cred; if (UIO_SEG_IS_USER_SPACE(segflg)) { spacetype = proc_is64bit(p) ? UIO_USERSPACE64 : UIO_USERSPACE32; } else { spacetype = UIO_SYSSPACE; } if (len < 0) { return EINVAL; } auio = uio_createwithbuffer(1, offset, spacetype, rw, &uio_buf[0], sizeof(uio_buf)); uio_addiov(auio, CAST_USER_ADDR_T(base), (user_size_t)len); #if CONFIG_MACF /* XXXMAC * IO_NOAUTH should be re-examined. * Likely that mediation should be performed in caller. */ if ((ioflg & IO_NOAUTH) == 0) { /* passed cred is fp->f_cred */ if (rw == UIO_READ) { error = mac_vnode_check_read(&context, cred, vp); } else { error = mac_vnode_check_write(&context, cred, vp); } } #endif if (error == 0) { if (rw == UIO_READ) { if (vnode_isswap(vp) && ((ioflg & IO_SWAP_DISPATCH) == 0)) { error = vn_read_swapfile(vp, auio); } else { error = VNOP_READ(vp, auio, ioflg, &context); } } else { error = VNOP_WRITE(vp, auio, ioflg, &context); } } if (aresid) { *aresid = uio_resid(auio); assert(*aresid <= len); } else if (uio_resid(auio) && error == 0) { error = EIO; } return error; } void vn_offset_lock(struct fileglob *fg) { lck_mtx_lock_spin(&fg->fg_lock); while (fg->fg_lflags & FG_OFF_LOCKED) { fg->fg_lflags |= FG_OFF_LOCKWANT; msleep(&fg->fg_lflags, &fg->fg_lock, PVFS | PSPIN, "fg_offset_lock_wait", 0); } fg->fg_lflags |= FG_OFF_LOCKED; lck_mtx_unlock(&fg->fg_lock); } void vn_offset_unlock(struct fileglob *fg) { int lock_wanted = 0; lck_mtx_lock_spin(&fg->fg_lock); if (fg->fg_lflags & FG_OFF_LOCKWANT) { lock_wanted = 1; } fg->fg_lflags &= ~(FG_OFF_LOCKED | FG_OFF_LOCKWANT); lck_mtx_unlock(&fg->fg_lock); if (lock_wanted) { wakeup(&fg->fg_lflags); } } static int vn_read_common(vnode_t vp, struct uio *uio, int fflag, vfs_context_t ctx) { int error; int ioflag; off_t read_offset; user_ssize_t read_len; user_ssize_t adjusted_read_len; user_ssize_t clippedsize; /* Caller has already validated read_len. */ read_len = uio_resid(uio); assert(read_len >= 0 && read_len <= INT_MAX); adjusted_read_len = read_len; clippedsize = 0; #if CONFIG_MACF error = mac_vnode_check_read(ctx, vfs_context_ucred(ctx), vp); if (error) { return error; } #endif /* This signals to VNOP handlers that this read came from a file table read */ ioflag = IO_SYSCALL_DISPATCH; if (fflag & FNONBLOCK) { ioflag |= IO_NDELAY; } if ((fflag & FNOCACHE) || vnode_isnocache(vp)) { ioflag |= IO_NOCACHE; } if (fflag & FENCRYPTED) { ioflag |= IO_ENCRYPTED; } if (fflag & FUNENCRYPTED) { ioflag |= IO_SKIP_ENCRYPTION; } if (fflag & O_EVTONLY) { ioflag |= IO_EVTONLY; } if (fflag & FNORDAHEAD) { ioflag |= IO_RAOFF; } read_offset = uio_offset(uio); /* POSIX allows negative offsets for character devices. */ if ((read_offset < 0) && (vnode_vtype(vp) != VCHR)) { error = EINVAL; goto error_out; } if (read_offset == INT64_MAX) { /* can't read any more */ error = 0; goto error_out; } /* * If offset + len will cause overflow, reduce the len to a value * (adjusted_read_len) where it won't */ if ((read_offset >= 0) && (INT64_MAX - read_offset) < read_len) { /* * 0 read_offset INT64_MAX * |-----------------------------------------------|----------|~~~ * <--read_len--> * <-adjusted-> */ adjusted_read_len = (user_ssize_t)(INT64_MAX - read_offset); } if (adjusted_read_len < read_len) { uio_setresid(uio, adjusted_read_len); clippedsize = read_len - adjusted_read_len; } if (vnode_isswap(vp) && !(IO_SKIP_ENCRYPTION & ioflag)) { /* special case for swap files */ error = vn_read_swapfile(vp, uio); } else { error = VNOP_READ(vp, uio, ioflag, ctx); } if (clippedsize) { uio_setresid(uio, (uio_resid(uio) + clippedsize)); } error_out: return error; } /* * File table vnode read routine. */ static int vn_read(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx) { vnode_t vp; user_ssize_t read_len; int error; bool offset_locked; read_len = uio_resid(uio); if (read_len < 0 || read_len > INT_MAX) { return EINVAL; } if ((flags & FOF_OFFSET) == 0) { vn_offset_lock(fp->fp_glob); offset_locked = true; } else { offset_locked = false; } vp = (struct vnode *)fp_get_data(fp); if ((error = vnode_getwithref(vp))) { if (offset_locked) { vn_offset_unlock(fp->fp_glob); } return error; } if (offset_locked && (vnode_vtype(vp) != VREG || vnode_isswap(vp))) { vn_offset_unlock(fp->fp_glob); offset_locked = false; } if ((flags & FOF_OFFSET) == 0) { uio_setoffset(uio, fp->fp_glob->fg_offset); } error = vn_read_common(vp, uio, fp->fp_glob->fg_flag, ctx); if ((flags & FOF_OFFSET) == 0) { fp->fp_glob->fg_offset += read_len - uio_resid(uio); } if (offset_locked) { vn_offset_unlock(fp->fp_glob); offset_locked = false; } vnode_put(vp); return error; } /* * File table vnode write routine. */ static int vn_write(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx) { struct vnode *vp; int error, ioflag; off_t write_offset; off_t write_end_offset; user_ssize_t write_len; user_ssize_t adjusted_write_len; user_ssize_t clippedsize; bool offset_locked; proc_t p = vfs_context_proc(ctx); rlim_t rlim_cur_fsize = p ? proc_limitgetcur(p, RLIMIT_FSIZE) : 0; write_len = uio_resid(uio); if (write_len < 0 || write_len > INT_MAX) { return EINVAL; } adjusted_write_len = write_len; clippedsize = 0; if ((flags & FOF_OFFSET) == 0) { vn_offset_lock(fp->fp_glob); offset_locked = true; } else { offset_locked = false; } vp = (struct vnode *)fp_get_data(fp); if ((error = vnode_getwithref(vp))) { if (offset_locked) { vn_offset_unlock(fp->fp_glob); } return error; } if (offset_locked && (vnode_vtype(vp) != VREG || vnode_isswap(vp))) { vn_offset_unlock(fp->fp_glob); offset_locked = false; } #if CONFIG_MACF error = mac_vnode_check_write(ctx, vfs_context_ucred(ctx), vp); if (error) { (void)vnode_put(vp); if (offset_locked) { vn_offset_unlock(fp->fp_glob); } return error; } #endif /* * IO_SYSCALL_DISPATCH signals to VNOP handlers that this write came from * a file table write */ ioflag = (IO_UNIT | IO_SYSCALL_DISPATCH); if (vp->v_type == VREG && (fp->fp_glob->fg_flag & O_APPEND)) { ioflag |= IO_APPEND; } if (fp->fp_glob->fg_flag & FNONBLOCK) { ioflag |= IO_NDELAY; } assert(!(fp->fp_glob->fg_flag & O_NOCTTY) || (fp->fp_glob->fg_flag & FNOCACHE)); if ((fp->fp_glob->fg_flag & FNOCACHE) || vnode_isnocache(vp)) { ioflag |= IO_NOCACHE; /* * O_NOCTTY is repurposed to indicate the preference for relaxed * alignment and size restrictions. O_NOCTTY has no meaning beyond * the open of the fd and is used only for ttys and the use * is mutually exclusive with nocache io for regular files. */ if (fp->fp_glob->fg_flag & O_NOCTTY || vfs_context_allow_fs_blksize_nocache_write(ctx)) { ioflag |= IO_NOCACHE_SWRITE; } } if (fp->fp_glob->fg_flag & FNODIRECT) { ioflag |= IO_NODIRECT; } if (fp->fp_glob->fg_flag & FSINGLE_WRITER) { ioflag |= IO_SINGLE_WRITER; } if (fp->fp_glob->fg_flag & O_EVTONLY) { ioflag |= IO_EVTONLY; } /* * Treat synchronous mounts and O_FSYNC on the fd as equivalent. * * XXX We treat O_DSYNC as O_FSYNC for now, since we can not delay * XXX the non-essential metadata without some additional VFS work; * XXX the intent at this point is to plumb the interface for it. */ if ((fp->fp_glob->fg_flag & (O_FSYNC | O_DSYNC)) || (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) { ioflag |= IO_SYNC; } if ((flags & FOF_OFFSET) == 0) { write_offset = fp->fp_glob->fg_offset; uio_setoffset(uio, write_offset); } else { /* for pwrite, append should be ignored */ ioflag &= ~IO_APPEND; write_offset = uio_offset(uio); /* POSIX allows negative offsets for character devices. */ if ((write_offset < 0) && (vnode_vtype(vp) != VCHR)) { error = EINVAL; goto error_out; } } if (write_offset == INT64_MAX) { /* writes are not possible */ error = EFBIG; goto error_out; } /* * write_len is the original write length that was requested. * We may however need to reduce that becasue of two reasons * * 1) If write_offset + write_len will exceed OFF_T_MAX (i.e. INT64_MAX) * and/or * 2) If write_offset + write_len will exceed the administrative * limit for the maximum file size. * * In both cases the write will be denied if we can't write even a single * byte otherwise it will be "clipped" (i.e. a short write). */ /* * If offset + len will cause overflow, reduce the len * to a value (adjusted_write_len) where it won't */ if ((write_offset >= 0) && (INT64_MAX - write_offset) < write_len) { /* * 0 write_offset INT64_MAX * |-----------------------------------------------|----------|~~~ * <--write_len--> * <-adjusted-> */ adjusted_write_len = (user_ssize_t)(INT64_MAX - write_offset); } /* write_end_offset will always be [0, INT64_MAX] */ write_end_offset = write_offset + adjusted_write_len; if (p && (vp->v_type == VREG) && (rlim_cur_fsize != RLIM_INFINITY) && (rlim_cur_fsize <= INT64_MAX) && (write_end_offset > (off_t)rlim_cur_fsize)) { /* * If the requested residual would cause us to go past the * administrative limit, then we need to adjust the residual * down to cause fewer bytes than requested to be written. If * we can't do that (e.g. the residual is already 1 byte), * then we fail the write with EFBIG. */ if (write_offset >= (off_t)rlim_cur_fsize) { /* * 0 rlim_fsize write_offset write_end INT64_MAX * |------------------------|----------|-------------|--------| * <--write_len--> * * write not permitted */ psignal(p, SIGXFSZ); error = EFBIG; goto error_out; } /* * 0 write_offset rlim_fsize write_end INT64_MAX * |------------------------|-----------|---------|------------| * <------write_len------> * <-adjusted--> */ adjusted_write_len = (user_ssize_t)((off_t)rlim_cur_fsize - write_offset); assert((adjusted_write_len > 0) && (adjusted_write_len < write_len)); } if (adjusted_write_len < write_len) { uio_setresid(uio, adjusted_write_len); clippedsize = write_len - adjusted_write_len; } error = VNOP_WRITE(vp, uio, ioflag, ctx); /* * If we had to reduce the size of write requested either because * of rlimit or because it would have exceeded * maximum file size, we have to add that back to the residual so * it correctly reflects what we did in this function. */ if (clippedsize) { uio_setresid(uio, (uio_resid(uio) + clippedsize)); } if ((flags & FOF_OFFSET) == 0) { if (ioflag & IO_APPEND) { fp->fp_glob->fg_offset = uio_offset(uio); } else { fp->fp_glob->fg_offset += (write_len - uio_resid(uio)); } if (offset_locked) { vn_offset_unlock(fp->fp_glob); offset_locked = false; } } /* * Set the credentials on successful writes */ if ((error == 0) && (vp->v_tag == VT_NFS) && (UBCINFOEXISTS(vp))) { ubc_setcred(vp, vfs_context_ucred(ctx)); } #if CONFIG_FILE_LEASES /* * On success, break the parent dir lease as the file's attributes (size * and/or mtime) have changed. Best attempt to break lease, just drop the * the error upon failure as there is no point to return error when the * write has completed successfully. */ if (__probable(error == 0)) { vnode_breakdirlease(vp, true, O_WRONLY); } #endif /* CONFIG_FILE_LEASES */ (void)vnode_put(vp); return error; error_out: if (offset_locked) { vn_offset_unlock(fp->fp_glob); } (void)vnode_put(vp); return error; } /* * File table vnode stat routine. * * Returns: 0 Success * EBADF * ENOMEM * vnode_getattr:??? */ int vn_stat_noauth(struct vnode *vp, void *sbptr, kauth_filesec_t *xsec, int isstat64, int needsrealdev, vfs_context_t ctx, struct ucred *file_cred) { struct vnode_attr va; int error; u_short mode; kauth_filesec_t fsec; struct stat *sb = (struct stat *)0; /* warning avoidance ; protected by isstat64 */ struct stat64 * sb64 = (struct stat64 *)0; /* warning avoidance ; protected by isstat64 */ if (isstat64 != 0) { sb64 = (struct stat64 *)sbptr; } else { sb = (struct stat *)sbptr; } memset(&va, 0, sizeof(va)); VATTR_INIT(&va); VATTR_WANTED(&va, va_fsid); VATTR_WANTED(&va, va_fileid); VATTR_WANTED(&va, va_mode); VATTR_WANTED(&va, va_type); VATTR_WANTED(&va, va_nlink); VATTR_WANTED(&va, va_uid); VATTR_WANTED(&va, va_gid); VATTR_WANTED(&va, va_rdev); VATTR_WANTED(&va, va_data_size); VATTR_WANTED(&va, va_access_time); VATTR_WANTED(&va, va_modify_time); VATTR_WANTED(&va, va_change_time); VATTR_WANTED(&va, va_create_time); VATTR_WANTED(&va, va_flags); VATTR_WANTED(&va, va_gen); VATTR_WANTED(&va, va_iosize); /* lower layers will synthesise va_total_alloc from va_data_size if required */ VATTR_WANTED(&va, va_total_alloc); if (xsec != NULL && !vnode_isnamedstream(vp)) { VATTR_WANTED(&va, va_uuuid); VATTR_WANTED(&va, va_guuid); VATTR_WANTED(&va, va_acl); } if (needsrealdev) { va.va_vaflags = VA_REALFSID; } error = vnode_getattr(vp, &va, ctx); if (error) { goto out; } #if CONFIG_MACF /* * Give MAC polices a chance to reject or filter the attributes * returned by the filesystem. Note that MAC policies are consulted * *after* calling the filesystem because filesystems can return more * attributes than were requested so policies wouldn't be authoritative * is consulted beforehand. This also gives policies an opportunity * to change the values of attributes retrieved. */ error = mac_vnode_check_getattr(ctx, file_cred, vp, &va); if (error) { goto out; } #endif /* * Copy from vattr table */ if (isstat64 != 0) { sb64->st_dev = va.va_fsid; sb64->st_ino = (ino64_t)va.va_fileid; } else { sb->st_dev = va.va_fsid; sb->st_ino = (ino_t)va.va_fileid; } mode = va.va_mode; switch (vp->v_type) { case VREG: mode |= S_IFREG; break; case VDIR: mode |= S_IFDIR; break; case VBLK: mode |= S_IFBLK; break; case VCHR: mode |= S_IFCHR; break; case VLNK: mode |= S_IFLNK; break; case VSOCK: mode |= S_IFSOCK; break; case VFIFO: mode |= S_IFIFO; break; default: error = EBADF; goto out; } ; if (isstat64 != 0) { sb64->st_mode = mode; sb64->st_nlink = VATTR_IS_SUPPORTED(&va, va_nlink) ? va.va_nlink > UINT16_MAX ? UINT16_MAX : (u_int16_t)va.va_nlink : 1; sb64->st_uid = va.va_uid; sb64->st_gid = va.va_gid; sb64->st_rdev = va.va_rdev; sb64->st_size = va.va_data_size; sb64->st_atimespec = va.va_access_time; sb64->st_mtimespec = va.va_modify_time; sb64->st_ctimespec = va.va_change_time; if (VATTR_IS_SUPPORTED(&va, va_create_time)) { sb64->st_birthtimespec = va.va_create_time; } else { sb64->st_birthtimespec.tv_sec = sb64->st_birthtimespec.tv_nsec = 0; } sb64->st_blksize = va.va_iosize; sb64->st_flags = va.va_flags; sb64->st_blocks = roundup(va.va_total_alloc, 512) / 512; } else { sb->st_mode = mode; sb->st_nlink = VATTR_IS_SUPPORTED(&va, va_nlink) ? va.va_nlink > UINT16_MAX ? UINT16_MAX : (u_int16_t)va.va_nlink : 1; sb->st_uid = va.va_uid; sb->st_gid = va.va_gid; sb->st_rdev = va.va_rdev; sb->st_size = va.va_data_size; sb->st_atimespec = va.va_access_time; sb->st_mtimespec = va.va_modify_time; sb->st_ctimespec = va.va_change_time; sb->st_blksize = va.va_iosize; sb->st_flags = va.va_flags; sb->st_blocks = roundup(va.va_total_alloc, 512) / 512; } /* if we're interested in extended security data and we got an ACL */ if (xsec != NULL) { if (!VATTR_IS_SUPPORTED(&va, va_acl) && !VATTR_IS_SUPPORTED(&va, va_uuuid) && !VATTR_IS_SUPPORTED(&va, va_guuid)) { *xsec = KAUTH_FILESEC_NONE; } else { if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL)) { fsec = kauth_filesec_alloc(va.va_acl->acl_entrycount); } else { fsec = kauth_filesec_alloc(0); } if (fsec == NULL) { error = ENOMEM; goto out; } fsec->fsec_magic = KAUTH_FILESEC_MAGIC; if (VATTR_IS_SUPPORTED(&va, va_uuuid)) { fsec->fsec_owner = va.va_uuuid; } else { fsec->fsec_owner = kauth_null_guid; } if (VATTR_IS_SUPPORTED(&va, va_guuid)) { fsec->fsec_group = va.va_guuid; } else { fsec->fsec_group = kauth_null_guid; } if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL)) { __nochk_bcopy(va.va_acl, &(fsec->fsec_acl), KAUTH_ACL_COPYSIZE(va.va_acl)); } else { fsec->fsec_acl.acl_entrycount = KAUTH_FILESEC_NOACL; } *xsec = fsec; } } /* Do not give the generation number out to unpriviledged users */ if (va.va_gen && !vfs_context_issuser(ctx)) { if (isstat64 != 0) { sb64->st_gen = 0; } else { sb->st_gen = 0; } } else { if (isstat64 != 0) { sb64->st_gen = va.va_gen; } else { sb->st_gen = va.va_gen; } } error = 0; out: if (VATTR_IS_SUPPORTED(&va, va_acl) && va.va_acl != NULL) { kauth_acl_free(va.va_acl); } return error; } int vn_stat(struct vnode *vp, void *sb, kauth_filesec_t *xsec, int isstat64, int needsrealdev, vfs_context_t ctx) { int error; #if CONFIG_MACF error = mac_vnode_check_stat(ctx, NOCRED, vp); if (error) { return error; } #endif /* authorize */ if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_READ_ATTRIBUTES | KAUTH_VNODE_READ_SECURITY, ctx)) != 0) { return error; } /* actual stat */ return vn_stat_noauth(vp, sb, xsec, isstat64, needsrealdev, ctx, NOCRED); } /* * File table vnode ioctl routine. */ static int vn_ioctl(struct fileproc *fp, u_long com, caddr_t data, vfs_context_t ctx) { struct vnode *vp = (struct vnode *)fp_get_data(fp); off_t file_size; int error; struct vnode *ttyvp; if ((error = vnode_getwithref(vp))) { return error; } #if CONFIG_MACF error = mac_vnode_check_ioctl(ctx, vp, com); if (error) { goto out; } #endif switch (vp->v_type) { case VREG: case VDIR: if (com == FIONREAD) { off_t temp_nbytes; if ((error = vnode_size(vp, &file_size, ctx)) != 0) { goto out; } temp_nbytes = file_size - fp->fp_glob->fg_offset; if (temp_nbytes > INT_MAX) { *(int *)data = INT_MAX; } else if (temp_nbytes < 0) { *(int *)data = 0; } else { *(int *)data = (int)temp_nbytes; } goto out; } if (com == FIONBIO || com == FIOASYNC) { /* XXX */ goto out; } OS_FALLTHROUGH; default: error = ENOTTY; goto out; case VFIFO: case VCHR: case VBLK: if (com == TIOCREVOKE || com == TIOCREVOKECLEAR) { error = ENOTTY; goto out; } /* Should not be able to set block size from user space */ if (com == DKIOCSETBLOCKSIZE) { error = EPERM; goto out; } /* Should not be able to check if filesystem is authenticated from user space */ if (com == FSIOC_AUTH_FS) { error = ENOTTY; goto out; } if (com == FIODTYPE) { if (vp->v_type == VBLK) { if (major(vp->v_rdev) >= nblkdev) { error = ENXIO; goto out; } *(int *)data = bdevsw[major(vp->v_rdev)].d_type; } else if (vp->v_type == VCHR) { if (major(vp->v_rdev) >= nchrdev) { error = ENXIO; goto out; } *(int *)data = cdevsw[major(vp->v_rdev)].d_type; } else { error = ENOTTY; goto out; } goto out; } error = VNOP_IOCTL(vp, com, data, fp->fp_glob->fg_flag, ctx); if (error == 0 && com == TIOCSCTTY) { struct session *sessp; struct pgrp *pg; pg = proc_pgrp(vfs_context_proc(ctx), &sessp); session_lock(sessp); ttyvp = sessp->s_ttyvp; sessp->s_ttyvp = vp; sessp->s_ttyvid = vnode_vid(vp); session_unlock(sessp); pgrp_rele(pg); } } out: (void)vnode_put(vp); return error; } /* * File table vnode select routine. */ static int vn_select(struct fileproc *fp, int which, void *wql, __unused vfs_context_t ctx) { int error; struct vnode * vp = (struct vnode *)fp_get_data(fp); struct vfs_context context; if ((error = vnode_getwithref(vp)) == 0) { context.vc_thread = current_thread(); context.vc_ucred = fp->fp_glob->fg_cred; #if CONFIG_MACF /* * XXX We should use a per thread credential here; minimally, * XXX the process credential should have a persistent * XXX reference on it before being passed in here. */ error = mac_vnode_check_select(ctx, vp, which); if (error == 0) #endif error = VNOP_SELECT(vp, which, fp->fp_glob->fg_flag, wql, ctx); (void)vnode_put(vp); } return error; } /* * File table vnode close routine. */ static int vn_closefile(struct fileglob *fg, vfs_context_t ctx) { struct vnode *vp = fg_get_data(fg); int error; if ((error = vnode_getwithref(vp)) == 0) { if (FILEGLOB_DTYPE(fg) == DTYPE_VNODE && ((fg->fg_flag & FWASLOCKED) != 0 || (fg->fg_lflags & FG_HAS_OFDLOCK) != 0)) { struct flock lf = { .l_whence = SEEK_SET, .l_start = 0, .l_len = 0, .l_type = F_UNLCK }; if ((fg->fg_flag & FWASLOCKED) != 0) { (void) VNOP_ADVLOCK(vp, (caddr_t)fg, F_UNLCK, &lf, F_FLOCK, ctx, NULL); } if ((fg->fg_lflags & FG_HAS_OFDLOCK) != 0) { (void) VNOP_ADVLOCK(vp, ofd_to_id(fg), F_UNLCK, &lf, F_OFD_LOCK, ctx, NULL); } } #if CONFIG_FILE_LEASES if (FILEGLOB_DTYPE(fg) == DTYPE_VNODE && !LIST_EMPTY(&vp->v_leases)) { /* Expected open count doesn't matter for release. */ (void)vnode_setlease(vp, fg, F_UNLCK, 0, ctx); } #endif error = vn_close(vp, fg->fg_flag, ctx); (void) vnode_put(vp); } return error; } /* * Returns: 0 Success * VNOP_PATHCONF:??? */ int vn_pathconf(vnode_t vp, int name, int32_t *retval, vfs_context_t ctx) { int error = 0; struct vfs_attr vfa; switch (name) { case _PC_EXTENDED_SECURITY_NP: *retval = vfs_extendedsecurity(vnode_mount(vp)) ? 1 : 0; break; case _PC_AUTH_OPAQUE_NP: *retval = vfs_authopaque(vnode_mount(vp)); break; case _PC_2_SYMLINKS: *retval = 1; /* XXX NOTSUP on MSDOS, etc. */ break; case _PC_ALLOC_SIZE_MIN: *retval = 1; /* XXX lie: 1 byte */ break; case _PC_ASYNC_IO: /* unistd.h: _POSIX_ASYNCHRONUS_IO */ *retval = 1; /* [AIO] option is supported */ break; case _PC_PRIO_IO: /* unistd.h: _POSIX_PRIORITIZED_IO */ *retval = 0; /* [PIO] option is not supported */ break; case _PC_REC_INCR_XFER_SIZE: *retval = 4096; /* XXX go from MIN to MAX 4K at a time */ break; case _PC_REC_MIN_XFER_SIZE: *retval = 4096; /* XXX recommend 4K minimum reads/writes */ break; case _PC_REC_MAX_XFER_SIZE: *retval = 65536; /* XXX recommend 64K maximum reads/writes */ break; case _PC_REC_XFER_ALIGN: *retval = 4096; /* XXX recommend page aligned buffers */ break; case _PC_SYMLINK_MAX: *retval = 255; /* Minimum acceptable POSIX value */ break; case _PC_SYNC_IO: /* unistd.h: _POSIX_SYNCHRONIZED_IO */ *retval = 0; /* [SIO] option is not supported */ break; case _PC_XATTR_SIZE_BITS: /* The number of bits used to store maximum extended * attribute size in bytes. For example, if the maximum * attribute size supported by a file system is 128K, the * value returned will be 18. However a value 18 can mean * that the maximum attribute size can be anywhere from * (256KB - 1) to 128KB. As a special case, the resource * fork can have much larger size, and some file system * specific extended attributes can have smaller and preset * size; for example, Finder Info is always 32 bytes. */ memset(&vfa, 0, sizeof(vfa)); VFSATTR_INIT(&vfa); VFSATTR_WANTED(&vfa, f_capabilities); if (vfs_getattr(vnode_mount(vp), &vfa, ctx) == 0 && (VFSATTR_IS_SUPPORTED(&vfa, f_capabilities)) && (vfa.f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_EXTENDED_ATTR) && (vfa.f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_EXTENDED_ATTR)) { /* Supports native extended attributes */ error = VNOP_PATHCONF(vp, name, retval, ctx); } else { /* Number of bits used to represent the maximum size of * extended attribute stored in an Apple Double file. */ *retval = AD_XATTR_SIZE_BITS; } break; default: error = VNOP_PATHCONF(vp, name, retval, ctx); break; } return error; } static int vn_kqfilter(struct fileproc *fp, struct knote *kn, struct kevent_qos_s *kev) { vfs_context_t ctx = vfs_context_current(); struct vnode *vp; int error = 0; int result = 0; vp = (struct vnode *)fp_get_data(fp); /* * Don't attach a knote to a dead vnode. */ if ((error = vget_internal(vp, 0, VNODE_NODEAD)) == 0) { switch (kn->kn_filter) { case EVFILT_READ: case EVFILT_WRITE: if (vnode_isfifo(vp)) { /* We'll only watch FIFOs that use our fifofs */ if (!(vp->v_fifoinfo && vp->v_fifoinfo->fi_readsock)) { error = ENOTSUP; } } else if (!vnode_isreg(vp)) { if (vnode_ischr(vp)) { result = spec_kqfilter(vp, kn, kev); if ((kn->kn_flags & EV_ERROR) == 0) { /* claimed by a special device */ vnode_put(vp); return result; } } error = EINVAL; } break; case EVFILT_VNODE: break; default: error = EINVAL; } if (error == 0) { #if CONFIG_MACF error = mac_vnode_check_kqfilter(ctx, fp->fp_glob->fg_cred, kn, vp); if (error) { vnode_put(vp); goto out; } #endif kn->kn_filtid = EVFILTID_VN; knote_kn_hook_set_raw(kn, (void *)vp); vnode_hold(vp); vnode_lock(vp); KNOTE_ATTACH(&vp->v_knotes, kn); result = filt_vnode_common(kn, NULL, vp, 0); vnode_unlock(vp); /* * Ask the filesystem to provide remove notifications, * but ignore failure */ VNOP_MONITOR(vp, 0, VNODE_MONITOR_BEGIN, (void*) kn, ctx); } vnode_put(vp); } out: if (error) { knote_set_error(kn, error); } return result; } static void filt_vndetach(struct knote *kn) { vfs_context_t ctx = vfs_context_current(); struct vnode *vp = (struct vnode *)knote_kn_hook_get_raw(kn); uint32_t vid = vnode_vid(vp); if (vnode_getwithvid(vp, vid)) { vnode_drop(vp); return; } vnode_drop(vp); vnode_lock(vp); KNOTE_DETACH(&vp->v_knotes, kn); vnode_unlock(vp); /* * Tell a (generally networked) filesystem that we're no longer watching * If the FS wants to track contexts, it should still be using the one from * the VNODE_MONITOR_BEGIN. */ VNOP_MONITOR(vp, 0, VNODE_MONITOR_END, (void*)kn, ctx); vnode_put(vp); } /* * Used for EVFILT_READ * * Takes only VFIFO or VREG. vnode is locked. We handle the "poll" case * differently than the regular case for VREG files. If not in poll(), * then we need to know current fileproc offset for VREG. */ static int64_t vnode_readable_data_count(vnode_t vp, off_t current_offset, int ispoll) { if (vnode_isfifo(vp)) { #if FIFO int cnt; int err = fifo_charcount(vp, &cnt); if (err == 0) { return (int64_t)cnt; } else #endif { return 0; } } else if (vnode_isreg(vp)) { if (ispoll) { return 1; } off_t amount; amount = vp->v_un.vu_ubcinfo->ui_size - current_offset; if (amount > INT64_MAX) { return INT64_MAX; } else if (amount < INT64_MIN) { return INT64_MIN; } else { return (int64_t)amount; } } else { panic("Should never have an EVFILT_READ except for reg or fifo."); return 0; } } /* * Used for EVFILT_WRITE. * * For regular vnodes, we can always write (1). For named pipes, * see how much space there is in the buffer. Nothing else is covered. */ static intptr_t vnode_writable_space_count(vnode_t vp) { if (vnode_isfifo(vp)) { #if FIFO long spc; int err = fifo_freespace(vp, &spc); if (err == 0) { return (intptr_t)spc; } else #endif { return (intptr_t)0; } } else if (vnode_isreg(vp)) { return (intptr_t)1; } else { panic("Should never have an EVFILT_READ except for reg or fifo."); return 0; } } /* * Determine whether this knote should be active * * This is kind of subtle. * --First, notice if the vnode has been revoked: in so, override hint * --EVFILT_READ knotes are checked no matter what the hint is * --Other knotes activate based on hint. * --If hint is revoke, set special flags and activate */ static int filt_vnode_common(struct knote *kn, struct kevent_qos_s *kev, vnode_t vp, long hint) { int activate = 0; int64_t data = 0; lck_mtx_assert(&vp->v_lock, LCK_MTX_ASSERT_OWNED); /* Special handling for vnodes that are in recycle or already gone */ if (NOTE_REVOKE == hint) { kn->kn_flags |= (EV_EOF | EV_ONESHOT); activate = 1; if ((kn->kn_filter == EVFILT_VNODE) && (kn->kn_sfflags & NOTE_REVOKE)) { kn->kn_fflags |= NOTE_REVOKE; } } else { switch (kn->kn_filter) { case EVFILT_READ: data = vnode_readable_data_count(vp, kn->kn_fp->fp_glob->fg_offset, (kn->kn_flags & EV_POLL)); activate = (data != 0); break; case EVFILT_WRITE: data = vnode_writable_space_count(vp); activate = (data != 0); break; case EVFILT_VNODE: /* Check events this note matches against the hint */ if (kn->kn_sfflags & hint) { kn->kn_fflags |= (uint32_t)hint; /* Set which event occurred */ } activate = (kn->kn_fflags != 0); break; default: panic("Invalid knote filter on a vnode!"); } } if (kev && activate) { knote_fill_kevent(kn, kev, data); } return activate; } static int filt_vnode(struct knote *kn, long hint) { vnode_t vp = (struct vnode *)knote_kn_hook_get_raw(kn); return filt_vnode_common(kn, NULL, vp, hint); } static int filt_vntouch(struct knote *kn, struct kevent_qos_s *kev) { vnode_t vp = (struct vnode *)knote_kn_hook_get_raw(kn); uint32_t vid = vnode_vid(vp); int activate; int hint = 0; vnode_lock(vp); if (vnode_getiocount(vp, vid, VNODE_NODEAD | VNODE_WITHID) != 0) { /* is recycled */ hint = NOTE_REVOKE; } /* accept new input fflags mask */ kn->kn_sfflags = kev->fflags; activate = filt_vnode_common(kn, NULL, vp, hint); if (hint == 0) { vnode_put_locked(vp); } vnode_unlock(vp); return activate; } static int filt_vnprocess(struct knote *kn, struct kevent_qos_s *kev) { vnode_t vp = (struct vnode *)knote_kn_hook_get_raw(kn); uint32_t vid = vnode_vid(vp); int activate; int hint = 0; vnode_lock(vp); if (vnode_getiocount(vp, vid, VNODE_NODEAD | VNODE_WITHID) != 0) { /* Is recycled */ hint = NOTE_REVOKE; } activate = filt_vnode_common(kn, kev, vp, hint); /* Definitely need to unlock, may need to put */ if (hint == 0) { vnode_put_locked(vp); } vnode_unlock(vp); return activate; } struct vniodesc { vnode_t vnio_vnode; /* associated vnode */ int vnio_fflags; /* cached fileglob flags */ }; errno_t vnio_openfd(int fd, vniodesc_t *vniop) { proc_t p = current_proc(); struct fileproc *fp = NULL; vniodesc_t vnio; vnode_t vp; int error; bool need_drop = false; vnio = kalloc_type(struct vniodesc, Z_WAITOK); error = fp_getfvp(p, fd, &fp, &vp); if (error) { goto out; } need_drop = true; if ((fp->fp_glob->fg_flag & (FREAD | FWRITE)) == 0) { error = EBADF; goto out; } if (vnode_isswap(vp)) { error = EPERM; goto out; } if (vp->v_type != VREG) { error = EFTYPE; goto out; } error = vnode_getwithref(vp); if (error) { goto out; } vnio->vnio_vnode = vp; vnio->vnio_fflags = fp->fp_glob->fg_flag; (void)fp_drop(p, fd, fp, 0); need_drop = false; error = vnode_ref_ext(vp, vnio->vnio_fflags, 0); if (error == 0) { *vniop = vnio; vnio = NULL; } vnode_put(vp); out: if (need_drop) { fp_drop(p, fd, fp, 0); } if (vnio != NULL) { kfree_type(struct vniodesc, vnio); } return error; } errno_t vnio_close(vniodesc_t vnio) { int error; /* * The vniodesc is always destroyed, because the close * always "succeeds". We just return whatever error * might have been encountered while doing so. */ error = vnode_getwithref(vnio->vnio_vnode); if (error == 0) { error = vnode_close(vnio->vnio_vnode, vnio->vnio_fflags, NULL); } kfree_type(struct vniodesc, vnio); return error; } errno_t vnio_read(vniodesc_t vnio, uio_t uio) { vnode_t vp = vnio->vnio_vnode; user_ssize_t read_len; int error; read_len = uio_resid(uio); if (read_len < 0 || read_len > INT_MAX) { return EINVAL; } error = vnode_getwithref(vp); if (error == 0) { error = vn_read_common(vp, uio, vnio->vnio_fflags, vfs_context_current()); vnode_put(vp); } return error; } vnode_t vnio_vnode(vniodesc_t vnio) { return vnio->vnio_vnode; } int vnode_isauthfs(vnode_t vp) { fsioc_auth_fs_t afs = { .authvp = NULL }; int error; error = VNOP_IOCTL(vp, FSIOC_AUTH_FS, (caddr_t)&afs, 0, vfs_context_current()); return error == 0 ? 1 : 0; } |