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
|
/* normalize.c --
* Copyright 2016-18,2021 Red Hat Inc.
* All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors:
* Steve Grubb <sgrubb@redhat.com>
*/
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <errno.h>
#include <pwd.h>
#include "libaudit.h"
#include "auparse.h"
#include "internal.h"
#include "normalize-llist.h"
#include "normalize-internal.h"
#include "gen_tables.h"
#include "normalize_record_maps.h"
#include "normalize_syscall_maps.h"
#include "normalize_obj_kind_maps.h"
#include "normalize_evtypetabs.h"
/*
* Field accessors. x is the new value, y is the variable
* Layout is: 0xFFFF FFFF where first is record and second is field
* Both record and field are 0 based. Simple records are always 0. Compound
* records start at 0 and go up.
*/
#define UNSET 0xFFFFU
#define get_record(y) ((y >> 16) & 0x0000FFFFU)
#define set_record(y, x) (((x & 0x0000FFFFU) << 16) | (y & 0x0000FFFFU))
#define get_field(y) (y & 0x0000FFFFU)
#define set_field(y, x) ((y & 0xFFFF0000U) | (x & 0x0000FFFFU))
#define is_unset(y) (get_record(y) == UNSET)
#define D au->norm_data
static int syscall_success;
static value_t find_simple_object(auparse_state_t *au, int type);
void init_normalizer(normalize_data *d)
{
d->evkind = NULL;
d->session = set_record(0, UNSET);
d->actor.primary = set_record(0, UNSET);
d->actor.secondary = set_record(0, UNSET);
d->actor.what = NULL;
cllist_create(&d->actor.attr, NULL);
d->action = NULL;
d->thing.primary = set_record(0, UNSET);
d->thing.secondary = set_record(0, UNSET);
d->thing.two = set_record(0, UNSET);
cllist_create(&d->thing.attr, NULL);
d->thing.what = NORM_WHAT_UNKNOWN;
d->results = set_record(0, UNSET);
d->how = NULL;
d->opt = NORM_OPT_ALL;
d->key = set_record(0, UNSET);
syscall_success = -1;
}
void clear_normalizer(normalize_data *d)
{
d->evkind = NULL;
d->session = set_record(0, UNSET);
d->actor.primary = set_record(0, UNSET);
d->actor.secondary = set_record(0, UNSET);
free((void *)d->actor.what);
d->actor.what = NULL;
cllist_clear(&d->actor.attr);
free((void *)d->action);
d->action = NULL;
d->thing.primary = set_record(0, UNSET);
d->thing.secondary = set_record(0, UNSET);
d->thing.two = set_record(0, UNSET);
cllist_clear(&d->thing.attr);
d->thing.what = NORM_WHAT_UNKNOWN;
d->results = set_record(0, UNSET);
free((void *)d->how);
d->how = NULL;
d->opt = NORM_OPT_ALL;
d->key = set_record(0, UNSET);
syscall_success = -1;
}
static void set_system_subject_what(auparse_state_t *au)
{
D.actor.what = strdup("system");
}
static void set_unknown_subject_what(auparse_state_t *au)
{
D.actor.what = strdup("unknown-acct");
}
static unsigned int set_subject_what(auparse_state_t *au)
{
int uid = NORM_ACCT_UNSET - 1;
int ftype = auparse_get_field_type(au);
if (ftype == AUPARSE_TYPE_UID)
uid = auparse_get_field_int(au);
else {
const char *n = auparse_get_field_name(au);
if (n && strcmp(n, "acct") == 0) {
const char *acct = auparse_interpret_field(au);
if (acct) {
uid_t lu = lookup_uid_from_name(au, acct);
if (lu != (uid_t)-1) {
uid = lu;
goto check;
}
}
}
set_unknown_subject_what(au);
return 1;
}
check:
if (uid == NORM_ACCT_PRIV)
D.actor.what = strdup("privileged-acct");
else if ((unsigned)uid == NORM_ACCT_UNSET)
D.actor.what = strdup("unset-acct");
else if (uid < NORM_ACCT_MAX_SYS)
D.actor.what = strdup("service-acct");
else if (uid < NORM_ACCT_MAX_USER)
D.actor.what = strdup("user-acct");
else
set_unknown_subject_what(au);
return 0;
}
static unsigned int set_prime_subject(auparse_state_t *au, const char *str,
unsigned int rnum)
{
if (auparse_find_field(au, str)) {
D.actor.primary = set_record(0, rnum);
D.actor.primary = set_field(D.actor.primary,
auparse_get_field_num(au));
return 0;
}
return 1;
}
static unsigned int set_secondary_subject(auparse_state_t *au, const char *str,
unsigned int rnum)
{
if (auparse_find_field(au, str)) {
D.actor.secondary = set_record(0, rnum);
D.actor.secondary = set_field(D.actor.secondary,
auparse_get_field_num(au));
return set_subject_what(au);
}
return 1;
}
static unsigned int add_subj_attr(auparse_state_t *au, const char *str,
unsigned int rnum)
{
value_t attr;
if ((auparse_find_field(au, str))) {
attr = set_record(0, rnum);
attr = set_field(attr, auparse_get_field_num(au));
if (cllist_append(&D.actor.attr, attr, NULL))
return 1;
return 0;
} else
auparse_goto_record_num(au, rnum);
return 1;
}
static unsigned int set_prime_object(auparse_state_t *au, const char *str,
unsigned int rnum)
{
if (auparse_find_field(au, str)) {
D.thing.primary = set_record(0, rnum);
D.thing.primary = set_field(D.thing.primary,
auparse_get_field_num(au));
return 0;
}
return 1;
}
static unsigned int set_prime_object2(auparse_state_t *au, const char *str,
unsigned int adjust)
{
unsigned int rnum = 2 + adjust;
auparse_goto_record_num(au, rnum);
auparse_first_field(au);
if (auparse_find_field(au, str)) {
D.thing.two = set_record(0, rnum);
D.thing.two = set_field(D.thing.two,
auparse_get_field_num(au));
return 0;
}
return 1;
}
static unsigned int add_obj_attr(auparse_state_t *au, const char *str,
unsigned int rnum)
{
value_t attr;
if ((auparse_find_field(au, str))) {
attr = set_record(0, rnum);
attr = set_field(attr, auparse_get_field_num(au));
if (cllist_append(&D.thing.attr, attr, NULL))
return 1;
return 0;
} else
auparse_goto_record_num(au, rnum);
return 1;
}
static unsigned int add_session(auparse_state_t *au, unsigned int rnum)
{
if (auparse_find_field(au, "ses")) {
D.session = set_record(0, rnum);
D.session = set_field(D.session,
auparse_get_field_num(au));
return 0;
} else
auparse_first_record(au);
return 1;
}
static unsigned int set_results(auparse_state_t *au, unsigned int rnum)
{
if (auparse_find_field(au, "res")) {
D.results = set_record(0, rnum);
D.results = set_field(D.results, auparse_get_field_num(au));
return 0;
}
return 1;
}
static void syscall_subj_attr(auparse_state_t *au)
{
unsigned int rnum;
auparse_first_record(au);
do {
rnum = auparse_get_record_num(au);
if (auparse_get_type(au) == AUDIT_SYSCALL) {
if (D.opt == NORM_OPT_NO_ATTRS) {
add_session(au, rnum);
return;
}
add_subj_attr(au, "ppid", rnum);
add_subj_attr(au, "pid", rnum);
add_subj_attr(au, "gid", rnum);
add_subj_attr(au, "euid", rnum);
add_subj_attr(au, "suid", rnum);
add_subj_attr(au, "fsuid", rnum);
add_subj_attr(au, "egid", rnum);
add_subj_attr(au, "sgid", rnum);
add_subj_attr(au, "fsgid", rnum);
add_subj_attr(au, "tty", rnum);
add_session(au, rnum);
add_subj_attr(au, "subj", rnum);
return;
}
} while (auparse_next_record(au) == 1);
}
static void collect_perm_obj2(auparse_state_t *au, const char *syscall)
{
const char *val;
if (strcmp(syscall, "fchmodat") == 0)
val = "a2";
else
val = "a1";
auparse_first_record(au);
if (auparse_find_field(au, val)) {
D.thing.two = set_record(0, 0);
D.thing.two = set_field(D.thing.two,
auparse_get_field_num(au));
}
}
static void collect_own_obj2(auparse_state_t *au, const char *syscall)
{
const char *val;
if (strcmp(syscall, "fchownat") == 0)
val = "a2";
else
val = "a1";
auparse_first_record(au);
if (auparse_find_field(au, val)) {
// if uid is -1, its not being changed, user group
if (auparse_get_field_int(au) == -1 && errno == 0)
auparse_next_field(au);
D.thing.two = set_record(0, 0);
D.thing.two = set_field(D.thing.two,
auparse_get_field_num(au));
}
}
static void collect_id_obj2(auparse_state_t *au, const char *syscall)
{
unsigned int limit, cnt = 1;
if (strcmp(syscall, "setuid") == 0)
limit = 1;
else if (strcmp(syscall, "setreuid") == 0)
limit = 2;
else if (strcmp(syscall, "setresuid") == 0)
limit = 3;
else if (strcmp(syscall, "setgid") == 0)
limit = 1;
else if (strcmp(syscall, "setregid") == 0)
limit = 2;
else if (strcmp(syscall, "setresgid") == 0)
limit = 3;
else
return; // Shouldn't happen
auparse_first_record(au);
if (auparse_find_field(au, "a0")) {
while (cnt <= limit) {
const char *str = auparse_interpret_field(au);
if ((strcmp(str, "unset") == 0) && errno == 0) {
// Only move it if its safe to
if (cnt < limit) {
if (auparse_next_field(au) == 0)
return;
cnt++;
} else
return;
} else
break;
}
D.thing.two = set_record(0, 0);
D.thing.two = set_field(D.thing.two,
auparse_get_field_num(au));
}
}
static int collect_path_attrs(auparse_state_t *au)
{
value_t attr;
unsigned int rnum = auparse_get_record_num(au);
auparse_first_field(au);
if (add_obj_attr(au, "mode", rnum))
return 1; // Failed opens don't have anything else
// All the rest of the fields matter
while ((auparse_next_field(au))) {
attr = set_record(0, rnum);
attr = set_field(attr, auparse_get_field_num(au));
if (cllist_append(&D.thing.attr, attr, NULL))
return 1;
}
return 0;
}
static void collect_cwd_attrs(auparse_state_t *au)
{
unsigned int rnum = auparse_get_record_num(au);
add_obj_attr(au, "cwd", rnum);
}
static void collect_sockaddr_attrs(auparse_state_t *au)
{
unsigned int rnum = auparse_get_record_num(au);
add_obj_attr(au, "saddr", rnum);
}
static void simple_file_attr(auparse_state_t *au)
{
int parent = 0;
if (D.opt == NORM_OPT_NO_ATTRS)
return;
auparse_first_record(au);
do {
const char *f;
int type = auparse_get_type(au);
switch (type)
{
case AUDIT_PATH:
f = auparse_find_field(au, "nametype");
if (f && strcmp(f, "PARENT") == 0) {
if (parent == 0)
parent = auparse_get_record_num(au);
continue;
}
// First normal record is collected
collect_path_attrs(au);
return;
break;
case AUDIT_CWD:
collect_cwd_attrs(au);
break;
case AUDIT_SOCKADDR:
collect_sockaddr_attrs(au);
break;
}
} while (auparse_next_record(au) == 1);
// If we get here, path was never collected. Go back and get parent
if (parent) {
auparse_goto_record_num(au, parent);
collect_path_attrs(au);
}
}
static void set_file_object(auparse_state_t *au, int adjust)
{
const char *f;
int parent = 0;
unsigned int rnum;
auparse_goto_record_num(au, 2 + adjust);
auparse_first_field(au);
// Now double check that we picked the right one.
do {
f = auparse_find_field(au, "nametype");
if (f) {
if (strcmp(f, "PARENT"))
break;
if (parent == 0)
parent = auparse_get_record_num(au);
}
} while (f && auparse_next_record(au) == 1);
// Sometimes we only have the parent (failed open at dir permission)
if (f == NULL) {
if (parent == 0)
return;
auparse_goto_record_num(au, parent);
auparse_first_field(au);
rnum = parent;
} else
rnum = auparse_get_record_num(au);
if (auparse_get_type(au) == AUDIT_PATH) {
auparse_first_field(au);
// Object
set_prime_object(au, "name", rnum);
f = auparse_find_field(au, "inode");
if (f) {
D.thing.secondary = set_record(0, rnum);
D.thing.secondary = set_field(D.thing.secondary,
auparse_get_field_num(au));
}
f = auparse_find_field(au, "mode");
if (f) {
unsigned int mode;
errno = 0;
mode = strtoul(f, NULL, 8);
if (errno == 0) {
if (S_ISREG(mode))
D.thing.what = NORM_WHAT_FILE;
else if (S_ISDIR(mode))
D.thing.what = NORM_WHAT_DIRECTORY;
else if (S_ISCHR(mode))
D.thing.what = NORM_WHAT_CHAR_DEV;
else if (S_ISBLK(mode))
D.thing.what = NORM_WHAT_BLOCK_DEV;
else if (S_ISFIFO(mode))
D.thing.what = NORM_WHAT_FIFO;
else if (S_ISLNK(mode))
D.thing.what = NORM_WHAT_LINK;
else if (S_ISSOCK(mode))
D.thing.what = NORM_WHAT_SOCKET;
}
}
}
}
static void set_socket_object(auparse_state_t *au)
{
auparse_goto_record_num(au, 1);
auparse_first_field(au);
set_prime_object(au, "saddr", 1);
}
/* This is only called processing syscall records */
static int set_program_obj(auparse_state_t *au)
{
auparse_first_record(au);
int type = auparse_get_type(au);
if (type == AUDIT_BPF) {
if (auparse_find_field(au, "prog-id")) {
D.thing.primary = set_record(0,
auparse_get_record_num(au));
D.thing.primary = set_field(D.thing.primary,
auparse_get_field_num(au));
}
} else if (type == AUDIT_EVENT_LISTENER) {
if (auparse_find_field(au, "nl-mcgrp")) {
D.thing.primary = set_record(0,
auparse_get_record_num(au));
D.thing.primary = set_field(D.thing.primary,
auparse_get_field_num(au));
}
} else if (type == AUDIT_MAC_POLICY_LOAD) {
if (auparse_find_field(au, "lsm")) {
D.thing.primary = set_record(0,
auparse_get_record_num(au));
D.thing.primary = set_field(D.thing.primary,
auparse_get_field_num(au));
}
} else if (auparse_find_field(au, "exe")) {
const char *exe = auparse_interpret_field(au);
if ((strncmp(exe, "/usr/bin/python", 15) == 0) ||
(strncmp(exe, "/usr/bin/sh", 11) == 0) ||
(strncmp(exe, "/usr/bin/bash", 13) == 0) ||
(strncmp(exe, "/usr/bin/perl", 13) == 0)) {
// comm should be the previous field
int fnum;
if ((fnum = auparse_get_field_num(au)) > 0)
auparse_goto_field_num(au, fnum - 1);
else
auparse_first_record(au);
auparse_find_field(au, "comm");
}
D.thing.primary = set_record(0,
auparse_get_record_num(au));
D.thing.primary = set_field(D.thing.primary,
auparse_get_field_num(au));
return 0;
}
return 1;
}
/*
* This function is supposed to come up with the action and object for the
* syscalls.
*/
static int normalize_syscall(auparse_state_t *au, const char *syscall)
{
int rc, tmp_objkind, objtype = NORM_UNKNOWN, ttype = 0, offset = 0;
const char *act = NULL, *f;
// cycle through all records and see what we have
tmp_objkind = objtype;
rc = auparse_first_record(au);
while (rc == 1) {
ttype = auparse_get_type(au);
if (ttype == AUDIT_AVC) {
// We want to go ahead with syscall to get objects
tmp_objkind = NORM_MAC;
break;
} else if (ttype == AUDIT_SELINUX_ERR) {
objtype = NORM_MAC_ERR;
break;
} else if (ttype == AUDIT_NETFILTER_CFG) {
objtype = NORM_IPTABLES;
break;
} else if (ttype == AUDIT_ANOM_PROMISCUOUS) {
objtype = NORM_PROMISCUOUS;
break;
} else if (ttype == AUDIT_KERN_MODULE) {
objtype = NORM_FILE_LDMOD;
break;
} else if (ttype == AUDIT_MAC_POLICY_LOAD) {
objtype = NORM_MAC_LOAD;
break;
} else if (ttype == AUDIT_MAC_STATUS) {
objtype = NORM_MAC_ENFORCE;
break;
} else if (ttype == AUDIT_MAC_CONFIG_CHANGE) {
objtype = NORM_MAC_CONFIG;
break;
} else if (ttype == AUDIT_FANOTIFY) {
// We want to go ahead with syscall to get objects
tmp_objkind = NORM_AV;
break;
} else if (ttype == AUDIT_TIME_INJOFFSET ||
ttype == AUDIT_TIME_ADJNTPVAL) {
objtype = NORM_SYSTEM_TIME;
break;
} else if (ttype == AUDIT_BPF) {
objtype = NORM_BPF;
break;
} else if (ttype == AUDIT_EVENT_LISTENER) {
objtype = NORM_EV_LISTEN;
break;
}
rc = auparse_next_record(au);
}
// lookup system call - it can be NULL if interpret_field failed. In
// that case, the s2i call will fail and leave objtype untouched
if (objtype == NORM_UNKNOWN)
normalize_syscall_map_s2i(syscall, &objtype);
switch (objtype)
{
case NORM_FILE:
act = "opened-file";
set_file_object(au, 0);
D.thing.what = NORM_WHAT_FILE; // this gets overridden
simple_file_attr(au);
break;
case NORM_FILE_CHATTR:
act = "changed-file-attributes-of";
D.thing.what = NORM_WHAT_FILE; // this gets overridden
if (strcmp(syscall, "fsetxattr") == 0)
offset = -1;
set_file_object(au, offset);
simple_file_attr(au);
break;
case NORM_FILE_CHPERM:
act = "changed-file-permissions-of";
D.thing.what = NORM_WHAT_FILE; // this gets overridden
if (strcmp(syscall, "fchmod") == 0)
offset = -1;
collect_perm_obj2(au, syscall);
set_file_object(au, offset);
simple_file_attr(au);
break;
case NORM_FILE_CHOWN:
act = "changed-file-ownership-of";
D.thing.what = NORM_WHAT_FILE; // this gets overridden
if (strcmp(syscall, "fchown") == 0) {
offset = -1;
collect_own_obj2(au, syscall);
/* fchown has no cwd or path information */
} else {
collect_own_obj2(au, syscall);
set_file_object(au, offset);
simple_file_attr(au);
}
break;
case NORM_FILE_LDMOD:
act = "loaded-kernel-module";
D.thing.what = NORM_WHAT_FILE;
auparse_goto_record_num(au, 1);
set_prime_object(au, "name", 1);
break;
case NORM_FILE_UNLDMOD:
act = "unloaded-kernel-module";
D.thing.what = NORM_WHAT_FILE; // this gets overridden
// set_file_object(au, 0);
// simple_file_attr(au);
break;
case NORM_FILE_DIR:
act = "created-directory";
D.thing.what = NORM_WHAT_FILE; // this gets overridden
set_file_object(au, 1); // New dir is one after
simple_file_attr(au);
break;
case NORM_FILE_MOUNT:
act = "mounted";
// this gets overridden
D.thing.what = NORM_WHAT_FILESYSTEM;
if (syscall_success == 1)
set_prime_object2(au, "name", 0);
//The device is 1 after on success 0 on fail
set_file_object(au, syscall_success);
// We call this directly to make sure the right
// PATH record is used. (There can be 4.)
collect_path_attrs(au);
break;
case NORM_FILE_RENAME:
act = "renamed";
D.thing.what = NORM_WHAT_FILE; // this gets overridden
/* A sucessfull syscall from the rename family will provide
* the following items:
* 0 - new dir, in which the file will be located
* 1 - old dir, in which the file was located
* 2 - old name, the name of the original file
* if the file was already present in the new dir:
* 3 - removal of the new file
* 4 - creation of the new file
* otherwise:
* 3 - creation of the new file
*/
// The 3rd record will always contain the name of the new file
set_prime_object2(au, "name", 3);
set_file_object(au, 2); // Thing renamed is 2 after
simple_file_attr(au);
break;
case NORM_FILE_STAT:
act = "checked-metadata-of";
D.thing.what = NORM_WHAT_FILE; // this gets overridden
set_file_object(au, 0);
simple_file_attr(au);
break;
case NORM_FILE_SYS_STAT:
act = "checked-filesystem-metadata-of";
D.thing.what = NORM_WHAT_FILESYSTEM; // this gets overridden
set_file_object(au, 0);
simple_file_attr(au);
break;
case NORM_FILE_LNK:
act = "symlinked";
D.thing.what = NORM_WHAT_FILE; // this gets overridden
set_prime_object2(au, "name", 0);
set_file_object(au, 2);
simple_file_attr(au);
break;
case NORM_FILE_UMNT:
act = "unmounted";
D.thing.what = NORM_WHAT_FILESYSTEM; // this gets overridden
set_file_object(au, 0);
simple_file_attr(au);
break;
case NORM_FILE_DEL:
act = "deleted";
D.thing.what = NORM_WHAT_FILE; // this gets overridden
set_file_object(au, 0);
simple_file_attr(au);
break;
case NORM_FILE_TIME:
act = "changed-timestamp-of";
D.thing.what = NORM_WHAT_FILE; // this gets overridden
set_file_object(au, 0);
simple_file_attr(au);
break;
case NORM_EXEC:
act = "executed";
D.thing.what = NORM_WHAT_FILE; // this gets overridden
set_file_object(au, 1);
simple_file_attr(au);
break;
case NORM_SOCKET_ACCEPT:
act = "accepted-connection-from";
D.thing.what = NORM_WHAT_SOCKET;
set_socket_object(au);
break;
case NORM_SOCKET_BIND:
act = "bound-socket";
D.thing.what = NORM_WHAT_SOCKET;
set_socket_object(au);
break;
case NORM_SOCKET_CONN:
act = "connected-to";
D.thing.what = NORM_WHAT_SOCKET;
set_socket_object(au);
break;
case NORM_SOCKET_RECV:
act = "received-from";
D.thing.what = NORM_WHAT_SOCKET;
set_socket_object(au);
break;
case NORM_SOCKET_SEND:
act = "sent-to";
D.thing.what = NORM_WHAT_SOCKET;
set_socket_object(au);
break;
case NORM_PID: {
unsigned int r, cnt;
value_t attr;
act = "killed-pid";
D.thing.what = NORM_WHAT_PROCESS;
// Loop to see how many OBJ_PID we have (normally 1)
cnt = auparse_get_num_records(au);
for (r = 1; r < cnt; r++) {
auparse_goto_record_num(au, r);
if (auparse_get_type(au) != AUDIT_OBJ_PID)
continue;
auparse_first_field(au);
if (auparse_find_field(au, "opid")) {
attr = set_record(0,
auparse_get_record_num(au));
attr = set_field(attr,
auparse_get_field_num(au));
cllist_append(&D.thing.attr, attr,
NULL);
}
}
// If there's more than one, it's a process group
if (D.thing.attr.cnt > 1) {
act = "killed-list-of-pids";
D.thing.what = NORM_WHAT_PROCESS_GROUP;
}
break;
}
case NORM_MAC_LOAD:
act = normalize_record_map_i2s(ttype);
// FIXME: What is the object?
D.thing.what = NORM_WHAT_SECURITY_POLICY;
break;
case NORM_MAC_CONFIG:
act = normalize_record_map_i2s(ttype);
f = auparse_find_field(au, "bool");
if (f) {
D.thing.primary = set_record(0,
auparse_get_record_num(au));
D.thing.primary = set_field(D.thing.primary,
auparse_get_field_num(au));
}
D.thing.what = NORM_WHAT_SECURITY_POLICY;
break;
case NORM_MAC_ENFORCE:
act = normalize_record_map_i2s(ttype);
f = auparse_find_field(au, "enforcing");
if (f) {
D.thing.primary = set_record(0,
auparse_get_record_num(au));
D.thing.primary = set_field(D.thing.primary,
auparse_get_field_num(au));
}
D.thing.what = NORM_WHAT_SECURITY_POLICY;
break;
case NORM_MAC_ERR:
// FIXME: What could the object be?
act = "caused-mac-policy-error";
// For now we'll call the obj_kind the system
D.thing.what = NORM_WHAT_SYSTEM;
break;
case NORM_IPTABLES:
act = "loaded-firewall-rule-to";
auparse_first_record(au);
f = auparse_find_field(au, "table");
if (f) {
D.thing.primary = set_record(0,
auparse_get_record_num(au));
D.thing.primary = set_field(D.thing.primary,
auparse_get_field_num(au));
}
D.thing.what = NORM_WHAT_FIREWALL;
break;
case NORM_PROMISCUOUS:
auparse_first_record(au);
f = auparse_find_field(au, "dev");
if (f) {
D.thing.primary = set_record(0,
auparse_get_record_num(au));
D.thing.primary = set_field(D.thing.primary,
auparse_get_field_num(au));
}
f = auparse_find_field(au, "prom");
if (f) {
int i = auparse_get_field_int(au);
if (i == 0)
act = "left-promiscuous-mode-on-device";
else
act = "entered-promiscuous-mode-on-device";
}
D.thing.what = NORM_WHAT_SOCKET;
break;
case NORM_UID:
case NORM_GID:
act = "changed-identity-of";
D.thing.what = NORM_WHAT_PROCESS;
set_program_obj(au);
if (D.how) {
free((void *)D.how);
D.how = strdup(syscall);
}
collect_id_obj2(au, syscall);
break;
case NORM_SYSTEM_TIME:
act = "changed-system-time";
// TODO: can't think of an object for this one
D.thing.what = NORM_WHAT_SYSTEM;
break;
case NORM_MAKE_DEV:
set_file_object(au, 0);
simple_file_attr(au);
if (D.thing.what == NORM_WHAT_CHAR_DEV)
act = "made-character-device";
else if (D.thing.what == NORM_WHAT_BLOCK_DEV)
act = "made-block-device";
else
act = "make-device";
break;
case NORM_SYSTEM_NAME:
act = "changed-system-name";
// TODO: can't think of an object for this one
D.thing.what = NORM_WHAT_SYSTEM;
break;
case NORM_SYSTEM_MEMORY:
act = "allocated-memory";
if (syscall_success == 1) {
// If its not a mmap avc, we can use comm
act = "allocated-memory-in";
auparse_first_record(au);
f = auparse_find_field(au, "comm");
if (f) {
D.thing.primary = set_record(0,
auparse_get_record_num(au));
D.thing.primary =
set_field(D.thing.primary,
auparse_get_field_num(au));
}
}
D.thing.what = NORM_WHAT_MEMORY;
break;
case NORM_SCHEDULER:
act = "adjusted-scheduling-policy-of";
D.thing.what = NORM_WHAT_PROCESS;
set_program_obj(au);
if (D.how) {
free((void *)D.how);
D.how = strdup(syscall);
}
break;
case NORM_BPF:
auparse_first_record(au);
f = auparse_find_field(au, "op");
if (f) {
const char *str = auparse_get_field_str(au);
if (strcmp(str, "LOAD") == 0)
act = "loaded-bpf-program";
else
act = "unloaded-bpf-program";
} else
act = "bpf-program";
D.thing.what = NORM_WHAT_PROCESS;
set_program_obj(au);
break;
case NORM_EV_LISTEN:
auparse_first_record(au);
f = auparse_find_field(au, "op");
if (f) {
const char *str = auparse_get_field_str(au);
if (strcmp(str, "connect") == 0)
act = "connected-to";
else
act = "disconnected-from";
} else
act = "connected";
D.thing.what = NORM_WHAT_SOCKET;
set_program_obj(au);
break;
case NORM_SECURITY_POLICY:
act = "adjusted-security-policy-of";
D.thing.what = NORM_WHAT_PROCESS;
set_program_obj(au);
if (D.how) {
free((void *)D.how);
D.how = strdup(syscall);
}
break;
case NORM_SECURITY_ATTR:
act = "retrieved-security-attr-of";
D.thing.what = NORM_WHAT_PROCESS;
set_program_obj(au);
break;
case NORM_SECURITY_LIST:
act = "listed-security-modules";
D.thing.what = NORM_WHAT_SECURITY_MODULES;
break;
default:
{
const char *k;
rc = auparse_first_record(au);
k = auparse_find_field(au, "key");
if (k && strcmp(k, "(null)")) {
act = "triggered-audit-rule";
D.thing.primary = set_record(0,
auparse_get_record_num(au));
D.thing.primary = set_field(
D.thing.primary,
auparse_get_field_num(au));
} else
act = "triggered-unknown-audit-rule";
D.thing.what = NORM_WHAT_AUDIT_RULE;
}
break;
}
// We put the AVC back after gathering the object information
if (tmp_objkind == NORM_MAC)
act = "accessed-mac-policy-controlled-object";
else if (tmp_objkind == NORM_AV)
act = "accessed-policy-controlled-file";
if (act)
D.action = strdup(act);
return 0;
}
static const char *normalize_determine_evkind(int type)
{
int kind;
switch (type)
{
case AUDIT_USER_AUTH ... AUDIT_USER_ACCT:
case AUDIT_CRED_ACQ ... AUDIT_USER_END:
case AUDIT_USER_CHAUTHTOK ... AUDIT_CRED_REFR:
case AUDIT_USER_LOGIN ... AUDIT_USER_LOGOUT:
case AUDIT_LOGIN:
kind = NORM_EVTYPE_USER_LOGIN;
break;
case AUDIT_GRP_AUTH:
case AUDIT_CHGRP_ID:
kind = NORM_EVTYPE_GROUP_CHANGE;
break;
case AUDIT_USER_MGMT:
case AUDIT_ADD_USER ...AUDIT_DEL_GROUP:
case AUDIT_GRP_MGMT ... AUDIT_GRP_CHAUTHTOK:
case AUDIT_ACCT_LOCK ... AUDIT_ACCT_UNLOCK:
kind = NORM_EVTYPE_USER_ACCT;
break;
case AUDIT_KERNEL:
case AUDIT_SYSTEM_BOOT ... AUDIT_SERVICE_STOP:
kind = NORM_EVTYPE_SYSTEM_SERVICES;
break;
case AUDIT_USYS_CONFIG:
case AUDIT_CONFIG_CHANGE:
case AUDIT_NETFILTER_CFG:
case AUDIT_FEATURE_CHANGE:
case AUDIT_TIME_INJOFFSET:
case AUDIT_TIME_ADJNTPVAL:
case AUDIT_USER_DEVICE:
case AUDIT_SOFTWARE_UPDATE:
kind = NORM_EVTYPE_CONFIG;
break;
case AUDIT_SECCOMP:
kind = NORM_EVTYPE_DAC_DECISION;
break;
case AUDIT_TEST ... AUDIT_TRUSTED_APP:
case AUDIT_USER_CMD:
case AUDIT_CHUSER_ID:
kind = NORM_EVTYPE_USERSPACE;
break;
case AUDIT_USER_TTY:
case AUDIT_TTY:
kind = NORM_EVTYPE_TTY;
break;
case AUDIT_EVENT_LISTENER:
case AUDIT_FIRST_DAEMON ... AUDIT_LAST_DAEMON:
kind = NORM_EVTYPE_AUDIT_DAEMON;
break;
case AUDIT_USER_SELINUX_ERR:
case AUDIT_USER_AVC:
case AUDIT_APPARMOR_ALLOWED ... AUDIT_APPARMOR_DENIED:
case AUDIT_APPARMOR_ERROR:
case AUDIT_AVC ... AUDIT_AVC_PATH:
kind = NORM_EVTYPE_MAC_DECISION;
break;
case AUDIT_INTEGRITY_FIRST_MSG ... AUDIT_INTEGRITY_LAST_MSG:
case AUDIT_ANOM_RBAC_INTEGRITY_FAIL: // Aide sends this
kind = NORM_EVTYPE_INTEGRITY;
break;
case AUDIT_FIRST_KERN_ANOM_MSG ... AUDIT_LAST_KERN_ANOM_MSG:
case AUDIT_FIRST_ANOM_MSG ... AUDIT_ANOM_RBAC_FAIL:
case AUDIT_ANOM_CRYPTO_FAIL ... AUDIT_LAST_ANOM_MSG:
kind = NORM_EVTYPE_ANOMALY;
break;
case AUDIT_FIRST_ANOM_RESP ... AUDIT_LAST_ANOM_RESP:
kind = NORM_EVTYPE_ANOMALY_RESP;
break;
case AUDIT_MAC_POLICY_LOAD ... AUDIT_LAST_SELINUX:
case AUDIT_AA ... AUDIT_APPARMOR_AUDIT:
case AUDIT_APPARMOR_HINT ... AUDIT_APPARMOR_STATUS:
case AUDIT_FIRST_USER_LSPP_MSG ... AUDIT_LAST_USER_LSPP_MSG:
kind = NORM_EVTYPE_MAC;
break;
case AUDIT_FIRST_KERN_CRYPTO_MSG ... AUDIT_LAST_KERN_CRYPTO_MSG:
case AUDIT_FIRST_CRYPTO_MSG ... AUDIT_LAST_CRYPTO_MSG:
kind = NORM_EVTYPE_CRYPTO;
break;
case AUDIT_FIRST_VIRT_MSG ... AUDIT_LAST_VIRT_MSG:
kind = NORM_EVTYPE_VIRT;
break;
case AUDIT_SYSCALL ... AUDIT_SOCKETCALL:
case AUDIT_SOCKADDR ... AUDIT_MQ_GETSETATTR:
case AUDIT_FD_PAIR ... AUDIT_OBJ_PID:
case AUDIT_BPRM_FCAPS ... AUDIT_NETFILTER_PKT:
case AUDIT_URINGOP:
kind = NORM_EVTYPE_AUDIT_RULE;
break;
case AUDIT_FANOTIFY:
kind = NORM_EVTYPE_AV_DECISION;
break;
case AUDIT_BPF:
kind = NORM_EVTYPE_BPF;
break;
default:
kind = NORM_EVTYPE_UNKNOWN;
}
return evtype_i2s(kind);
}
static const char *find_config_change_object(auparse_state_t *au)
{
const char *f;
// Check if its an audit rule
auparse_first_record(au);
f = auparse_find_field(au, "key");
if (f) {
const char *str = auparse_get_field_str(au);
if (str && strcmp(str, "(null)"))
return f;
}
// Next lets find the individual objects being set
auparse_first_record(au);
f = auparse_find_field(au, "audit_enabled");
if (f)
return f;
auparse_first_record(au);
f = auparse_find_field(au, "audit_pid");
if (f)
return f;
auparse_first_record(au);
f = auparse_find_field(au, "audit_backlog_limit");
if (f)
return f;
auparse_first_record(au);
f = auparse_find_field(au, "audit_failure");
if (f)
return f;
auparse_first_record(au);
f = auparse_find_field(au, "actions"); // seccomp-logging
if (f)
return f;
auparse_first_record(au);
f = auparse_find_field(au, "list"); // If nothing else, the list
if (f)
return f;
return NULL;
}
static int normalize_compound(auparse_state_t *au)
{
const char *f, *syscall = NULL;
int rc, recno, otype, type;
otype = type = auparse_get_type(au);
// All compound events have a syscall record, find it. After this
// loop, type should be syscall, and otype is the original type.
// Traditionally, the first record is the purpose of the event and
// the syscall is added on next to support/enhance information content.
if (type != AUDIT_SYSCALL) {
do {
// If we go off the end without finding a syscall
// record, don't parse corrupt events
if (auparse_next_record(au) < 0)
return 1;
type = auparse_get_type(au);
} while (type && type != AUDIT_SYSCALL);
}
if (!type)
return 1;
// Determine the kind of event using original event type
D.evkind = normalize_determine_evkind(otype);
if (type == AUDIT_SYSCALL) {
recno = auparse_get_record_num(au);
f = auparse_find_field(au, "syscall");
if (f) {
f = auparse_interpret_field(au);
if (f)
syscall = strdup(f);
}
// Results
f = auparse_find_field(au, "success");
if (f) {
const char *str = auparse_get_field_str(au);
if (strcmp(str, "no") == 0)
syscall_success = 0;
else
syscall_success = 1;
D.results = set_record(0, recno);
D.results = set_field(D.results,
auparse_get_field_num(au));
} else {
rc = auparse_goto_record_num(au, recno);
if (rc != 1) {
free((void *)syscall);
return 1;
}
auparse_first_field(au);
}
// Subject - primary
if (set_prime_subject(au, "auid", recno)) {
rc = auparse_goto_record_num(au, recno);
if (rc != 1) {
free((void *)syscall);
return 1;
}
auparse_first_field(au);
}
// Subject - alias, uid comes before auid
if (set_secondary_subject(au, "uid", recno)) {
rc = auparse_goto_record_num(au, recno);
if (rc != 1) {
free((void *)syscall);
return 1;
}
auparse_first_field(au);
}
// Subject attributes
syscall_subj_attr(au);
// how
auparse_first_field(au);
f = auparse_find_field(au, "exe");
if (f) {
const char *exe = auparse_interpret_field(au);
D.how = strdup(exe);
if (D.how == NULL) {
fprintf(stderr, "Out of memory. Check %s file, %d line", __FILE__, __LINE__);
free((void *)syscall);
return 1;
}
if ((strncmp(D.how, "/usr/bin/python", 15) == 0) ||
(strncmp(D.how, "/usr/bin/sh", 11) == 0) ||
(strncmp(D.how, "/usr/bin/bash", 13) == 0) ||
(strncmp(D.how, "/usr/bin/perl", 13) == 0)) {
int fnum;
rc = 0;
// Comm should be the previous field
if ((fnum = auparse_get_field_num(au)) > 0)
rc = auparse_goto_field_num(au,fnum-1);
if (rc == 0)
auparse_first_record(au);
f = auparse_find_field(au, "comm");
if (f) {
free((void *)D.how);
exe = auparse_interpret_field(au);
D.how = strdup(exe);
}
}
} else {
rc = auparse_goto_record_num(au, recno);
if (rc != 1) {
free((void *)syscall);
return 1;
}
auparse_first_field(au);
}
f = auparse_find_field(au, "key");
if (f) {
const char *k = auparse_get_field_str(au);
if (strcmp(k, "(null)")) {
// We only collect real keys
D.key = set_record(0, recno);
D.key = set_field(D.key,
auparse_get_field_num(au));
}
} // No error repositioning will be done because nothing
// below uses fields.
// action & object
if (otype == AUDIT_ANOM_LINK) {
const char *act = normalize_record_map_i2s(otype);
if (act)
D.action = strdup(act);
set_file_object(au, 1);
if (is_unset(D.thing.primary)) {
int r, num = auparse_get_num_records(au);
for (r = 1; r <= num; r++) {
auparse_goto_record_num(au, r);
if (auparse_get_type(au) == AUDIT_PATH) {
auparse_first_field(au);
set_prime_object(au, "name", r);
D.thing.what = NORM_WHAT_LINK;
break;
}
}
if (is_unset(D.thing.primary)) {
auparse_first_record(au);
f = auparse_find_field(au, "path");
if (f == NULL)
f = auparse_find_field(au, "cwd");
if (f) {
D.thing.primary = set_record(0,
auparse_get_record_num(au));
D.thing.primary =
set_field(D.thing.primary,
auparse_get_field_num(au));
D.thing.what = NORM_WHAT_LINK;
}
}
}
} else if (otype == AUDIT_CONFIG_CHANGE) {
auparse_first_record(au);
f = auparse_find_field(au, "op");
if (f) {
value_t o;
// Fix the action
D.action = strdup(auparse_interpret_field(au));
// Next fix the object
o = find_simple_object(au, AUDIT_CONFIG_CHANGE);
D.thing.primary = o;
}
} else {
normalize_syscall(au, syscall);
if (otype == AUDIT_MAC_POLICY_LOAD)
set_program_obj(au);
}
}
free((void *)syscall);
return 0;
}
static value_t find_simple_object(auparse_state_t *au, int type)
{
value_t o = set_record(0, UNSET);
const char *f = NULL;
auparse_first_field(au);
switch (type)
{
case AUDIT_SERVICE_START:
case AUDIT_SERVICE_STOP:
f = auparse_find_field(au, "unit");
D.thing.what = NORM_WHAT_SERVICE;
break;
case AUDIT_SYSTEM_RUNLEVEL:
f = auparse_find_field(au, "new-level");
D.thing.what = NORM_WHAT_SYSTEM;
break;
case AUDIT_USER_ROLE_CHANGE:
f = auparse_find_field(au, "selected-context");
D.thing.what = NORM_WHAT_USER_SESSION;
break;
case AUDIT_ROLE_ASSIGN:
case AUDIT_ROLE_REMOVE:
case AUDIT_USER_MGMT:
case AUDIT_ACCT_LOCK:
case AUDIT_ACCT_UNLOCK:
case AUDIT_ADD_USER:
case AUDIT_DEL_USER:
case AUDIT_ADD_GROUP:
case AUDIT_DEL_GROUP:
case AUDIT_GRP_MGMT:
f = auparse_find_field(au, "id");
if (f == NULL) {
auparse_first_record(au);
f = auparse_find_field(au, "acct");
}
D.thing.what = NORM_WHAT_ACCT;
break;
case AUDIT_USER_START:
case AUDIT_USER_END:
case AUDIT_USER_ERR:
case AUDIT_USER_LOGIN:
case AUDIT_USER_LOGOUT:
f = auparse_find_field(au, "terminal");
D.thing.what = NORM_WHAT_USER_SESSION;
break;
case AUDIT_USER_AUTH:
case AUDIT_USER_ACCT:
case AUDIT_CRED_ACQ:
case AUDIT_CRED_REFR:
case AUDIT_CRED_DISP:
case AUDIT_USER_CHAUTHTOK:
case AUDIT_GRP_CHAUTHTOK:
case AUDIT_ANOM_LOGIN_FAILURES:
case AUDIT_ANOM_LOGIN_TIME:
case AUDIT_ANOM_LOGIN_SESSIONS:
case AUDIT_ANOM_LOGIN_LOCATION:
f = auparse_find_field(au, "acct");
D.thing.what = NORM_WHAT_USER_SESSION;
break;
case AUDIT_ANOM_EXEC:
case AUDIT_USER_CMD:
f = auparse_find_field(au, "cmd");
D.thing.what = NORM_WHAT_PROCESS;
break;
case AUDIT_USER_TTY:
case AUDIT_TTY:
auparse_first_record(au);
f = auparse_find_field(au, "data");
D.thing.what = NORM_WHAT_KEYSTROKES;
break;
case AUDIT_USER_DEVICE:
auparse_first_record(au);
f = auparse_find_field(au, "device");
D.thing.what = NORM_WHAT_KEYSTROKES;
break;
case AUDIT_SOFTWARE_UPDATE:
auparse_first_record(au);
f = auparse_find_field(au, "sw");
D.thing.what = NORM_WHAT_SOFTWARE;
break;
case AUDIT_VIRT_MACHINE_ID:
f = auparse_find_field(au, "vm");
D.thing.what = NORM_WHAT_VM;
break;
case AUDIT_VIRT_RESOURCE:
f = auparse_find_field(au, "resrc");
D.thing.what = NORM_WHAT_VM;
break;
case AUDIT_VIRT_CONTROL:
f = auparse_find_field(au, "op");
D.thing.what = NORM_WHAT_VM;
break;
case AUDIT_LABEL_LEVEL_CHANGE:
f = auparse_find_field(au, "printer");
D.thing.what = NORM_WHAT_PRINTER;
break;
case AUDIT_CONFIG_CHANGE:
f = find_config_change_object(au);
D.thing.what = NORM_WHAT_AUDIT_CONFIG;
break;
case AUDIT_MAC_CONFIG_CHANGE:
f = auparse_find_field(au, "bool");
D.thing.what = NORM_WHAT_SECURITY_POLICY;
break;
case AUDIT_MAC_STATUS:
f = auparse_find_field(au, "enforcing");
D.thing.what = NORM_WHAT_SECURITY_POLICY;
break;
// These deal with policy, not sure about object yet
case AUDIT_MAC_POLICY_LOAD:
case AUDIT_LABEL_OVERRIDE:
case AUDIT_DEV_ALLOC ... AUDIT_USER_MAC_CONFIG_CHANGE:
D.thing.what = NORM_WHAT_SECURITY_POLICY;
break;
case AUDIT_USER:
f = auparse_find_field(au, "addr");
// D.thing.what = NORM_WHAT_?
break;
case AUDIT_USYS_CONFIG:
f = auparse_find_field(au, "op");
if (f) {
free((void *)D.action);
D.action = strdup(auparse_interpret_field(au));
f = NULL;
}
D.thing.what = NORM_WHAT_SYSTEM;
break;
case AUDIT_CRYPTO_KEY_USER:
f = auparse_find_field(au, "fp");
D.thing.what = NORM_WHAT_USER_SESSION;
break;
case AUDIT_CRYPTO_SESSION:
f = auparse_find_field(au, "addr");
D.thing.what = NORM_WHAT_USER_SESSION;
break;
case AUDIT_ANOM_RBAC_INTEGRITY_FAIL:
f = auparse_find_field(au, "hostname");
D.thing.what = NORM_WHAT_FILESYSTEM;
break;
default:
break;
}
if (f) {
o = set_record(0, 0);
o = set_field(o, auparse_get_field_num(au));
}
return o;
}
static value_t find_simple_obj_secondary(auparse_state_t *au, int type)
{
value_t o = set_record(0, UNSET);
const char *f = NULL;
auparse_first_field(au);
switch (type)
{
case AUDIT_CRYPTO_SESSION:
f = auparse_find_field(au, "rport");
break;
case AUDIT_SOFTWARE_UPDATE:
f = auparse_find_field(au, "sw_type");
break;
default:
break;
}
if (f) {
o = set_record(0, 0);
o = set_field(o, auparse_get_field_num(au));
}
return o;
}
static value_t find_simple_obj_primary2(auparse_state_t *au, int type)
{
value_t o = set_record(0, UNSET);
const char *f = NULL;
auparse_first_field(au);
switch (type)
{
case AUDIT_VIRT_CONTROL:
f = auparse_find_field(au, "vm");
break;
case AUDIT_VIRT_RESOURCE:
f = auparse_find_field(au, "vm");
break;
case AUDIT_SOFTWARE_UPDATE:
f = auparse_find_field(au, "root_dir");
break;
default:
break;
}
if (f) {
o = set_record(0, 0);
o = set_field(o, auparse_get_field_num(au));
}
return o;
}
static void collect_simple_subj_attr(auparse_state_t *au)
{
if (D.opt == NORM_OPT_NO_ATTRS)
return;
auparse_first_record(au);
add_subj_attr(au, "pid", 0); // Just pass 0 since simple is 1 record
add_subj_attr(au, "subj", 0);
}
static void collect_userspace_subj_attr(auparse_state_t *au, int type)
{
if (D.opt == NORM_OPT_NO_ATTRS)
return;
// Just pass 0 since simple is 1 record
add_subj_attr(au, "hostname", 0);
add_subj_attr(au, "addr", 0);
// Some events have the terminal as the object - skip for them
if (type != AUDIT_USER_START && type != AUDIT_USER_END &&
type != AUDIT_USER_ERR)
add_subj_attr(au, "terminal", 0);
}
static int normalize_simple(auparse_state_t *au)
{
const char *f, *act = NULL;
int type = auparse_get_type(au);
// Some older OS do not have PROCTITLE records
if (type == AUDIT_SYSCALL)
return normalize_compound(au);
// Determine the kind of event
D.evkind = normalize_determine_evkind(type);
// This is for events that follow:
// auid, (op), (uid), stuff
if (type == AUDIT_CONFIG_CHANGE || type == AUDIT_FEATURE_CHANGE ||
type == AUDIT_SECCOMP || type == AUDIT_ANOM_ABEND ||
type == AUDIT_ANOM_PROMISCUOUS) {
// Subject - primary
set_prime_subject(au, "auid", 0);
// Session
add_session(au, 0);
// Subject attrs
collect_simple_subj_attr(au);
// action
if (type == AUDIT_CONFIG_CHANGE) {
auparse_first_field(au);
f = auparse_find_field(au, "op");
if (f) {
const char *str = auparse_interpret_field(au);
if (*str == '"')
str++;
if (strncmp(str, "add_rule", 8) == 0) {
D.action = strdup("added-audit-rule");
D.thing.primary =
find_simple_object(au, type);
} else if (strncmp(str,"remove_rule",11) == 0){
D.action = strdup("deleted-audit-rule");
D.thing.primary =
find_simple_object(au, type);
} else
goto map;
} else
goto map;
} else { // This assigns action for feature_change, seccomp,
// and anom_abend
map:
act = normalize_record_map_i2s(type);
if (act)
D.action = strdup(act);
if (type == AUDIT_CONFIG_CHANGE)
D.thing.primary = find_simple_object(au, type);
auparse_first_record(au);
}
// object
if (type == AUDIT_FEATURE_CHANGE) {
// Subject - secondary
auparse_first_field(au);
if (set_secondary_subject(au, "uid", 0))
auparse_first_record(au);
// how
f = auparse_find_field(au, "exe");
if (f) {
const char *sig = auparse_interpret_field(au);
D.how = strdup(sig);
}
// object
set_prime_object(au, "feature", 0);
D.thing.what = NORM_WHAT_SYSTEM;
}
if (type == AUDIT_SECCOMP) {
// Subject - secondary
auparse_first_field(au);
if (set_secondary_subject(au, "uid", 0))
auparse_first_record(au);
// how
f = auparse_find_field(au, "exe");
if (f) {
const char *sig = auparse_interpret_field(au);
D.how = strdup(sig);
}
// Object
if (set_prime_object(au, "syscall", 0))
auparse_first_record(au);
D.thing.what = NORM_WHAT_PROCESS;
// Results
f = auparse_find_field(au, "code");
if (f) {
D.results = set_record(0, 0);
D.results = set_field(D.results,
auparse_get_field_num(au));
}
return 0;
}
if (type == AUDIT_ANOM_ABEND) {
// Subject - secondary
auparse_first_field(au);
if (set_secondary_subject(au, "uid", 0))
auparse_first_record(au);
//object
if (set_prime_object(au, "exe", 0))
auparse_first_record(au);
D.thing.what = NORM_WHAT_PROCESS;
// how
f = auparse_find_field(au, "sig");
if (f) {
const char *sig = auparse_interpret_field(au);
D.how = strdup(sig);
}
}
if (type == AUDIT_ANOM_PROMISCUOUS) {
auparse_first_field(au);
set_prime_object(au, "dev", 0);
set_secondary_subject(au, "uid", 0);
D.thing.what = NORM_WHAT_SOCKET;
}
// Results
set_results(au, 0);
return 0;
}
// This one is atypical and originates from the kernel
if (type == AUDIT_LOGIN) {
// Secondary
if (set_secondary_subject(au, "uid", 0))
auparse_first_record(au);
// Subject attrs
collect_simple_subj_attr(au);
// Subject
if (set_prime_subject(au, "old-auid", 0))
auparse_first_record(au);
// Object
if (set_prime_object(au, "auid", 0))
auparse_first_record(au);
D.thing.what = NORM_WHAT_USER_SESSION;
// Session
add_session(au, 0);
// Results
set_results(au, 0);
// action
act = normalize_record_map_i2s(type);
if (act)
D.action = strdup(act);
// How - currently missing
return 0;
}
// NETFILTER_CFG is atypical
if (type == AUDIT_NETFILTER_CFG) {
// Subject attrs
collect_simple_subj_attr(au);
// how
f = auparse_find_field(au, "comm");
if (f) {
const char *sig = auparse_interpret_field(au);
D.how = strdup(sig);
}
D.action = strdup("loaded-firewall-rule-to");
auparse_first_record(au);
f = auparse_find_field(au, "table");
if (f) {
D.thing.primary = set_record(0,
auparse_get_record_num(au));
D.thing.primary = set_field(D.thing.primary,
auparse_get_field_num(au));
}
set_system_subject_what(au);
D.thing.what = NORM_WHAT_FIREWALL;
return 0;
}
/* This one is also atypical and comes from the kernel */
if (type == AUDIT_AVC) {
// how
f = auparse_find_field(au, "comm");
if (f) {
const char *sig = auparse_interpret_field(au);
D.how = strdup(sig);
} else
auparse_first_record(au);
// Subject
set_prime_subject(au, "scontext", 0);
set_unknown_subject_what(au);
auparse_first_record(au);
// Object
if (D.opt == NORM_OPT_ALL) {
// We will only collect this when everything is asked
// for because it messes up text format otherwise
set_prime_object(au, "tcontext", 0);
auparse_first_record(au);
}
// Ideally we would choose tclass
D.thing.what = NORM_WHAT_UNKNOWN;
// action
act = normalize_record_map_i2s(type);
if (act)
D.action = strdup(act);
// find the denial
auparse_first_record(au);
f = auparse_find_field(au, "seresult");
if (f) {
D.results = set_record(0, 0);
D.results = set_field(D.results,
auparse_get_field_num(au));
}
// This is slim pickings without a syscall record
return 0;
}
/* Daemon events are atypical because they never transit the kernel */
if (type >= AUDIT_FIRST_DAEMON &&
type < AUDIT_LAST_DAEMON) {
// Subject - primary
set_prime_subject(au, "auid", 0);
// Secondary - optional
if (set_secondary_subject(au, "uid", 0))
auparse_first_record(au);
// Session - optional
if (add_session(au, 0))
auparse_first_record(au);
// Subject attrs
collect_simple_subj_attr(au);
free((void *)D.actor.what);
D.actor.what = strdup("auditd");
// action
act = normalize_record_map_i2s(type);
if (act)
D.action = strdup(act);
// Object type
D.thing.what = NORM_WHAT_SERVICE;
// How start:init, everything else:signal
if (type == AUDIT_DAEMON_START)
D.how = strdup("init");
else if (type < AUDIT_DAEMON_ACCEPT && type != AUDIT_DAEMON_ABORT)
D.how = strdup("signal");
// Results
set_results(au, 0);
return 0;
}
// BPF events are atypical
if (type == AUDIT_BPF) {
set_system_subject_what(au);
auparse_first_record(au);
f = auparse_find_field(au, "op");
if (f) {
const char *str = auparse_get_field_str(au);
if (strcmp(str, "LOAD") == 0)
act = "loaded-bpf-program";
else
act = "unloaded-bpf-program";
} else
act = "bpf-program";
D.action = strdup(act);
D.thing.what = NORM_WHAT_PROCESS;
set_program_obj(au);
return 0;
}
// LISTENER events are atypical
if (type == AUDIT_EVENT_LISTENER) {
// Subject - primary
set_prime_subject(au, "auid", 0);
// Secondary - optional
auparse_first_record(au);
set_secondary_subject(au, "uid", 0);
// Session
auparse_first_record(au);
add_session(au, 0);
// Subject attrs
collect_simple_subj_attr(au);
auparse_first_record(au);
f = auparse_find_field(au, "op");
if (f) {
const char *str = auparse_get_field_str(au);
if (strcmp(str, "connect") == 0)
act = "connected-to";
else
act = "disconnected-from";
} else
act = "connected";
D.action = strdup(act);
set_program_obj(au);
D.thing.what = NORM_WHAT_SOCKET;
// How
auparse_first_record(au);
f = auparse_find_field(au, "exe");
if (f) {
const char *exe = auparse_interpret_field(au);
D.how = strdup(exe);
}
// Results
auparse_first_record(au);
set_results(au, 0);
return 0;
}
// Labeled networking events are atypical
if (type >= AUDIT_MAC_UNLBL_ALLOW && type <= AUDIT_LAST_SELINUX) {
// Subject - primary
set_prime_subject(au, "auid", 0);
// We don't have a secondary subject, so set it to auid
set_subject_what(au);
// Session
add_session(au, 0);
// Subject attrs
add_subj_attr(au, "subj", 0);
// action
if (type == AUDIT_MAC_UNLBL_ALLOW) {
f = auparse_find_field(au, "unlbl_accept");
if (f) {
if (auparse_get_field_int(au) == 1)
act = "is-allowing-unlabeled-network-traffic";
else
act = "is-disallowing-unlabeled-network-traffic";
} else
auparse_first_record(au);
} else
act = normalize_record_map_i2s(type);
if (act)
D.action = strdup(act);
if (type == AUDIT_MAC_MAP_ADD || type == AUDIT_MAC_MAP_DEL) {
if (set_prime_object(au, "nlbl_domain", 0))
auparse_first_record(au);
}
// Object type
D.thing.what = NORM_WHAT_SECURITY_POLICY;
// Results
set_results(au, 0);
return 0;
}
// This is for events that follow:
// uid, auid, ses, res, find_simple_object
//
// USER_LOGIN is different in locating the subject because if they
// fail login, they are not quite in the system to have an auid.
if (type == AUDIT_USER_LOGIN) {
// Subject - primary
if (set_prime_subject(au, "id", 0)) {
auparse_first_record(au);
if (set_prime_subject(au, "acct", 0) == 0)
set_subject_what(au);
} else // If id found, set the subjkind
set_subject_what(au);
auparse_first_record(au);
} else {
// Subject - alias, uid comes before auid
if (set_secondary_subject(au, "uid", 0))
auparse_first_record(au);
// Subject - primary
set_prime_subject(au, "auid", 0);
}
// Session
add_session(au, 0);
// Subject attrs
collect_simple_subj_attr(au);
if ((type >= AUDIT_FIRST_USER_MSG && type < AUDIT_LAST_USER_MSG) ||
(type >= AUDIT_FIRST_USER_MSG2 && type < AUDIT_LAST_USER_MSG2))
collect_userspace_subj_attr(au, type);
// Results
if (type != AUDIT_USER_AVC)
set_results(au, 0);
else {
// find the denial
auparse_first_record(au);
f = auparse_find_field(au, "seresult");
if (f) {
D.results = set_record(0, 0);
D.results = set_field(D.results,
auparse_get_field_num(au));
}
// Subject
auparse_first_record(au);
set_prime_subject(au, "scontext", 0);
// Object
if (D.opt == NORM_OPT_ALL) {
// We will only collect this when everything is asked
// for because it messes up text format otherwise
auparse_first_record(au);
set_prime_object(au, "tcontext", 0);
}
}
// action
if (type == AUDIT_USER_DEVICE) {
auparse_first_record(au);
f = auparse_find_field(au, "op");
if (f)
act = f;
}
if (act == NULL)
act = normalize_record_map_i2s(type);
if (act)
D.action = strdup(act);
// object
if (type != AUDIT_USER_AVC) {
auparse_first_record(au);
D.thing.primary = find_simple_object(au, type);
D.thing.secondary = find_simple_obj_secondary(au, type);
D.thing.two = find_simple_obj_primary2(au, type);
// object attrs - rare on simple events
if (D.opt == NORM_OPT_ALL) {
if (type == AUDIT_USER_DEVICE) {
add_obj_attr(au, "uuid", 0);
} else if (type == AUDIT_SOFTWARE_UPDATE) {
auparse_first_record(au);
add_obj_attr(au, "key_enforce", 0);
add_obj_attr(au, "gpg_res", 0);
}
}
}
// how
if (type == AUDIT_SYSTEM_BOOT) {
D.thing.what = NORM_WHAT_SYSTEM;
f = auparse_find_field(au, "exe");
if (f) {
const char *exe = auparse_interpret_field(au);
D.how = strdup(exe);
}
return 0;
} else if (type == AUDIT_SYSTEM_SHUTDOWN) {
D.thing.what = NORM_WHAT_SERVICE;
f = auparse_find_field(au, "exe");
if (f) {
const char *exe = auparse_interpret_field(au);
D.how = strdup(exe);
}
return 0;
}
auparse_first_record(au);
if (type == AUDIT_ANOM_EXEC) {
f = auparse_find_field(au, "terminal");
if (f) {
const char *term = auparse_interpret_field(au);
D.how = strdup(term);
}
return 0;
}
if (type == AUDIT_TTY) {
f = auparse_find_field(au, "comm");
if (f) {
const char *comm = auparse_interpret_field(au);
D.how = strdup(comm);
}
return 0;
}
f = auparse_find_field(au, "exe");
if (f) {
const char *exe = auparse_interpret_field(au);
D.how = strdup(exe);
if (D.how == NULL) {
fprintf(stderr, "Out of memory. Check %s file, %d line", __FILE__, __LINE__);
return 1;
}
if ((strncmp(D.how, "/usr/bin/python", 15) == 0) ||
(strncmp(D.how, "/usr/bin/sh", 11) == 0) ||
(strncmp(D.how, "/usr/bin/bash", 13) == 0) ||
(strncmp(D.how, "/usr/bin/perl", 13) == 0)) {
// comm should be the previous field if its there at all
int fnum;
if ((fnum = auparse_get_field_num(au)) > 0)
auparse_goto_field_num(au, fnum - 1);
else
auparse_first_record(au);
f = auparse_find_field(au, "comm");
if (f) {
free((void *)D.how);
exe = auparse_interpret_field(au);
D.how = strdup(exe);
}
}
}
return 0;
}
/*
* This is the main entry point for the normalization. This function
* will analyze the current event to pick out the important pieces.
*/
int auparse_normalize(auparse_state_t *au, normalize_option_t opt)
{
int rc;
unsigned num;
auparse_first_record(au);
num = auparse_get_num_records(au);
// Reset cursor - no idea what we are being handed
auparse_first_record(au);
clear_normalizer(&D);
D.opt = opt;
// If we have more than one record in the event its a syscall based
// event. Otherwise its a simple event with all pieces in the same
// record.
if (num > 1)
rc = normalize_compound(au);
else
rc = normalize_simple(au);
// Reset the cursor
auparse_first_record(au);
return rc;
}
/*
* This function positions the internal cursor to the record and field that
* the location refers to.
* Returns: < 0 error, 0 uninitialized, 1 == success
*/
static int seek_field(auparse_state_t *au, value_t location)
{
int record, field, rc;
if (is_unset(location))
return 0;
record = get_record(location);
field = get_field(location);
rc = auparse_goto_record_num(au, record);
if (rc != 1)
return -1;
rc = auparse_goto_field_num(au, field);
if (rc != 1)
return -2;
return 1;
}
const char *auparse_normalize_get_event_kind(const auparse_state_t *au)
{
return D.evkind;
}
int auparse_normalize_session(auparse_state_t *au)
{
return seek_field(au, D.session);
}
int auparse_normalize_subject_primary(auparse_state_t *au)
{
return seek_field(au, D.actor.primary);
}
int auparse_normalize_subject_secondary(auparse_state_t *au)
{
return seek_field(au, D.actor.secondary);
}
const char *auparse_normalize_subject_kind(const auparse_state_t *au)
{
return D.actor.what;
}
// Returns: -1 = error, 0 uninitialized, 1 == success
int auparse_normalize_subject_first_attribute(auparse_state_t *au)
{
if (D.actor.attr.cnt) {
data_node *n;
cllist_first(&D.actor.attr);
n = cllist_get_cur(&D.actor.attr);
if (n)
return seek_field(au, n->num);
}
return 0;
}
// Returns: -1 = error, 0 uninitialized, 1 == success
int auparse_normalize_subject_next_attribute(auparse_state_t *au)
{
if (D.actor.attr.cnt) {
data_node *n;
n = cllist_next(&D.actor.attr);
if (n)
return seek_field(au, n->num);
}
return 0;
}
const char *auparse_normalize_get_action(const auparse_state_t *au)
{
return D.action;
}
int auparse_normalize_object_primary(auparse_state_t *au)
{
return seek_field(au, D.thing.primary);
}
int auparse_normalize_object_secondary(auparse_state_t *au)
{
return seek_field(au, D.thing.secondary);
}
int auparse_normalize_object_primary2(auparse_state_t *au)
{
return seek_field(au, D.thing.two);
}
// Returns: -1 = error, 0 uninitialized, 1 == success
int auparse_normalize_object_first_attribute(auparse_state_t *au)
{
if (D.thing.attr.cnt) {
data_node *n;
cllist_first(&D.thing.attr);
n = cllist_get_cur(&D.thing.attr);
if (n)
return seek_field(au, n->num);
}
return 0;
}
// Returns: -1 = error, 0 uninitialized, 1 == success
int auparse_normalize_object_next_attribute(auparse_state_t *au)
{
if (D.thing.attr.cnt) {
data_node *n;
n = cllist_next(&D.thing.attr);
if (n)
return seek_field(au, n->num);
}
return 0;
}
const char *auparse_normalize_object_kind(const auparse_state_t *au)
{
return normalize_obj_kind_map_i2s(D.thing.what);
}
int auparse_normalize_object_kind_int(const auparse_state_t *au)
{
return D.thing.what;
}
int auparse_normalize_get_results(auparse_state_t *au)
{
return seek_field(au, D.results);
}
const char *auparse_normalize_how(const auparse_state_t *au)
{
return D.how;
}
int auparse_normalize_key(auparse_state_t *au)
{
return seek_field(au, D.key);
}
|