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
|
/* Copyright (C) 2003-2004 Tresys Technology, LLC
* see file 'COPYING' for use and warranty information */
/*
* Author: kmacmillan@tresys.com
* Modified by: mayerf@tresys.com (Apr 2004) - separated information
* flow from main analysis.c file, and added noflow/onlyflow batch
* capabilitiy.
*/
/* infoflow.c
*
* Information Flow analysis routines for libapol
*/
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <time.h>
#include "policy.h"
#include "util.h"
#include "infoflow.h"
#include "policy-query.h"
#include "queue.h"
/*
* Nodes in the graph represent either a type used in the source
* of an allow rule or the target: these defines are used to
* represent which.
*/
#define IFLOW_SOURCE_NODE 0x0
#define IFLOW_TARGET_NODE 0x1
/*
* These defines are used to color nodes in the graph algorithms.
*/
#define IFLOW_COLOR_WHITE 0
#define IFLOW_COLOR_GREY 1
#define IFLOW_COLOR_BLACK 2
#define IFLOW_COLOR_RED 3
typedef struct iflow_edge {
int num_rules;
int *rules;
int start_node; /* index into iflow_graph->nodes */
int end_node; /* index into iflow_graph->nodes */
int length;
} iflow_edge_t;
typedef struct iflow_node {
int type;
int node_type;
int obj_class;
int num_in_edges;
int *in_edges;
int num_out_edges;
int *out_edges;
unsigned char color;
int parent;
int distance;
} iflow_node_t;
typedef struct iflow_graph {
int num_nodes; /* the number of slots used in nodes */
iflow_node_t *nodes;
int *src_index;
int *tgt_index;
int num_edges;
iflow_edge_t *edges;
policy_t *policy;
iflow_query_t *query;
} iflow_graph_t;
/* iflow_query_t */
iflow_query_t *iflow_query_create(void)
{
iflow_query_t* q = (iflow_query_t*)malloc(sizeof(iflow_query_t));
if (q == NULL) {
fprintf(stderr, "Memory error!\n");
return NULL;
}
memset(q, 0, sizeof(iflow_query_t));
q->start_type = -1;
q->direction = IFLOW_IN;
return q;
}
static int iflow_obj_options_copy(obj_perm_set_t *dest, obj_perm_set_t *src)
{
dest->obj_class = src->obj_class;
dest->num_perms = src->num_perms;
if (src->num_perms) {
assert(src->perms);
if (copy_int_array(&dest->perms, src->perms, src->num_perms))
return -1;
}
return 0;
}
/* perform a deep copy of an iflow_query_t - dest should be
* a newly created iflow_query */
static int iflow_query_copy(iflow_query_t *dest, iflow_query_t *src)
{
int i;
assert(dest && src);
dest->start_type = src->start_type;
dest->direction = src->direction;
if (src->num_end_types) {
assert(src->end_types);
if (copy_int_array(&dest->end_types, src->end_types, src->num_end_types))
return -1;
dest->num_end_types = src->num_end_types;
}
if (src->num_types) {
assert(src->types);
if (copy_int_array(&dest->types, src->types, src->num_types))
return -1;
dest->num_types = src->num_types;
}
if (src->num_obj_options) {
assert(src->obj_options);
dest->obj_options = (obj_perm_set_t*)malloc(sizeof(obj_perm_set_t) *
src->num_obj_options);
if (!dest->obj_options) {
fprintf(stderr, "Memory error\n");
return -1;
}
memset(dest->obj_options, 0, sizeof(obj_perm_set_t) * src->num_obj_options);
for (i = 0; i < src->num_obj_options; i++) {
if (iflow_obj_options_copy(dest->obj_options + i, src->obj_options + i))
return -1;
}
dest->num_obj_options = src->num_obj_options;
}
return 0;
}
void iflow_query_destroy(iflow_query_t *q)
{
int i;
if (q->end_types)
free(q->end_types);
if (q->types)
free(q->types);
for (i = 0; i < q->num_obj_options; i++) {
if (q->obj_options[i].perms)
free(q->obj_options[i].perms);
}
if (q->obj_options)
free(q->obj_options);
free(q);
}
/* Object class filter macros.
* Transitive information flow - if perms is non-NULL then only those
* permissions are ignored, otherwise the entire object class is ignored.
*/
int iflow_query_add_obj_class(iflow_query_t *q, int obj_class)
{
return apol_add_class_to_obj_perm_set_list(&q->obj_options, &q->num_obj_options, obj_class);
}
int iflow_query_add_obj_class_perm(iflow_query_t *q, int obj_class, int perm)
{
return apol_add_perm_to_obj_perm_set_list(&q->obj_options, &q->num_obj_options, obj_class, perm);
}
int iflow_query_add_end_type(iflow_query_t *q, int end_type)
{
return policy_query_add_type(&q->end_types, &q->num_end_types, end_type);
}
int iflow_query_add_type(iflow_query_t *q, int type)
{
return policy_query_add_type(&q->types, &q->num_types, type);
}
/*
* Check that the iflow_obj_option_t is valid for the graph/policy.
*/
bool_t iflow_obj_option_is_valid(obj_perm_set_t *o, policy_t *policy)
{
int i;
assert(o && policy);
if (!is_valid_obj_class_idx(o->obj_class, policy))
return FALSE;
if (o->num_perms) {
if (!o->perms) {
fprintf(stderr, "query with num_perms %d and perms is NULL\n", o->num_perms);
return FALSE;
}
for (i = 0; i < o->num_perms; i++) {
if (!is_valid_perm_for_obj_class(policy, o->obj_class, o->perms[i])) {
fprintf(stderr, "query with invalid perm %d for object class %d\n",
o->perms[i], o->obj_class);
return FALSE;
}
}
}
return TRUE;
}
/* check to make certain that a query is consistent and makes
* sense with the graph/policy */
bool_t iflow_query_is_valid(iflow_query_t *q, policy_t *policy)
{
int i;
#ifdef DEBUG_QUERIES
printf("start type: %s\n", policy->types[q->start_type].name);
printf("types[%d]:\n", q->num_types);
for (i = 0; i < q->num_types; i++)
printf("\t%s\n", policy->types[q->types[i]].name);
printf("end types[%d]: \n", q->num_end_types);
for (i = 0; i < q->num_end_types; i++)
printf("\t%s\n", policy->types[q->end_types[i]].name);
printf("obj options[%d]: \n", q->num_obj_options);
for (i = 0; i < q->num_obj_options; i++) {
int j;
printf("\tobj class [%d]%s perms [%d]:\n", q->obj_options[i].obj_class,
policy->obj_classes[q->obj_options[i].obj_class].name,
q->obj_options[i].num_perms);
for (j = 0; j < q->obj_options[i].num_perms; j++)
printf("\t\t%s\n", policy->perms[q->obj_options[i].perms[j]]);
}
#endif
/* check the start type - we don't allow self (which is always 0) */
if (!is_valid_type(policy, q->start_type, FALSE)) {
fprintf(stderr, "invalid start type %d in query\n", q->start_type);
return FALSE;
}
/* transitive analysis will have to do further checks */
if (!(q->direction == IFLOW_IN || q->direction == IFLOW_OUT
|| q->direction == IFLOW_BOTH || q->direction == IFLOW_EITHER)) {
fprintf(stderr, "invalid direction %d in query\n", q->direction);
return FALSE;
}
if (q->num_end_types) {
if (!q->end_types) {
fprintf(stderr, "query num_end_types was %d but end_types was NULL\n",
q->num_end_types);
return FALSE;
}
for (i = 0; i < q->num_end_types; i++) {
if (!is_valid_type(policy, q->end_types[i], FALSE)) {
fprintf(stderr, "Invalid end type %d in query\n", q->end_types[i]);
return FALSE;
}
}
}
if (q->num_types) {
if (!q->types) {
fprintf(stderr, "query num_types was %d but types was NULL\n",
q->num_types);
return FALSE;
}
for (i = 0; i < q->num_types; i++) {
if (!is_valid_type(policy, q->types[i], FALSE)) {
fprintf(stderr, "Invalid end type %d in query\n", q->types[i]);
return FALSE;
}
}
}
if (q->num_obj_options) {
if (!q->obj_options) {
fprintf(stderr, "query num_obj_options was %d by obj_options was NULL\n",
q->num_obj_options);
return FALSE;
}
for (i = 0; i < q->num_obj_options; i++) {
if (!iflow_obj_option_is_valid(&q->obj_options[i], policy)) {
return FALSE;
}
}
}
return TRUE;
}
/* iflow_t */
int iflow_init(iflow_graph_t *g, iflow_t *flow)
{
memset(flow, 0, sizeof(iflow_t));
flow->num_obj_classes = g->policy->num_obj_classes;
flow->obj_classes = (iflow_obj_class_t*)malloc(sizeof(iflow_obj_class_t) *
flow->num_obj_classes);
if (!flow->obj_classes) {
fprintf(stderr, "Memory Error\n");
return -1;
}
memset(flow->obj_classes, 0, sizeof(iflow_obj_class_t) *
flow->num_obj_classes);
return 0;
}
static void iflow_destroy_data(iflow_t *flow)
{
int i;
if (flow->obj_classes) {
for (i = 0; i < flow->num_obj_classes; i++) {
if (flow->obj_classes[i].rules)
free(flow->obj_classes[i].rules);
}
free(flow->obj_classes);
}
}
void iflow_destroy(iflow_t *flow)
{
if (!flow)
return;
iflow_destroy_data(flow);
free(flow);
}
/* iflow_transitive_t */
static void iflow_path_destroy(iflow_path_t *path)
{
int i;
if (!path)
return;
for (i = 0; i < path->num_iflows; i++) {
iflow_destroy_data(&path->iflows[i]);
}
if (path->iflows)
free(path->iflows);
free(path);
}
static void iflow_path_destroy_list(iflow_path_t *path)
{
iflow_path_t *next;
while (path) {
next = path->next;
iflow_path_destroy(path);
path = next;
}
}
void iflow_transitive_destroy(iflow_transitive_t *flow)
{
int i;
if (!flow)
return;
if (flow->end_types)
free(flow->end_types);
for (i = 0; i < flow->num_end_types; i++) {
iflow_path_destroy_list(flow->paths[i]);
}
if (flow->paths)
free(flow->paths);
if (flow->num_paths)
free(flow->num_paths);
free(flow);
}
/* iflow_node_t */
static void iflow_node_destroy_data(iflow_node_t *node)
{
if (!node)
return;
if (node->in_edges)
free(node->in_edges);
if (node->out_edges)
free(node->out_edges);
}
/* iflow_graph_t */
#define get_src_index(type) type
#define get_tgt_index(g, type, obj_class) ((type * g->policy->num_obj_classes) + obj_class)
static iflow_graph_t *iflow_graph_alloc(policy_t *policy)
{
iflow_graph_t *g;
int index_size;
g = (iflow_graph_t*)malloc(sizeof(iflow_graph_t));
if (!g) {
fprintf(stderr, "Memory error\n");
return NULL;
}
memset(g, 0, sizeof(iflow_graph_t));
index_size = policy->num_types;
g->src_index = (int*)malloc(sizeof(int) * index_size);
if (!g->src_index) {
fprintf(stderr, "Memory error\n");
return NULL;
}
memset(g->src_index, -1, sizeof(int) * index_size);
index_size = policy->num_types * policy->num_obj_classes;
g->tgt_index = (int*)malloc(sizeof(int) * index_size);
if (!g->tgt_index) {
fprintf(stderr, "Memory error\n");
return NULL;
}
memset(g->tgt_index, -1, sizeof(int) * index_size);
g->policy = policy;
return g;
}
void iflow_graph_destroy(iflow_graph_t *g)
{
int i;
if (!g)
return;
for (i = 0; i < g->num_nodes; i++)
iflow_node_destroy_data(&g->nodes[i]);
if (g->src_index)
free(g->src_index);
if (g->tgt_index)
free(g->tgt_index);
if (g->nodes)
free(g->nodes);
if (g->edges) {
for (i = 0; i < g->num_edges; i++) {
if (g->edges[i].rules)
free(g->edges[i].rules);
}
free(g->edges);
}
}
static int iflow_graph_get_nodes_for_type(iflow_graph_t *g, int type, int *len, int **types)
{
int i;
*len = 0;
*types = NULL;
if (g->src_index[get_src_index(type)] >= 0)
if (add_i_to_a(g->src_index[get_src_index(type)], len, types) < 0)
return -1;
for (i = 0; i < g->policy->num_obj_classes; i++) {
if (g->tgt_index[get_tgt_index(g, type, i)] >= 0)
if (add_i_to_a(g->tgt_index[get_tgt_index(g, type, i)], len, types) < 0)
return -1;
}
return 0;
}
static int iflow_graph_connect(iflow_graph_t *g, int start_node, int end_node, int length)
{
iflow_node_t* start, *end;
int i;
start = &g->nodes[start_node];
end = &g->nodes[end_node];
for (i = 0; i < start->num_out_edges; i++) {
if (g->edges[start->out_edges[i]].end_node == end_node) {
if (g->edges[start->out_edges[i]].length < length)
g->edges[start->out_edges[i]].length = length;
return start->out_edges[i];
}
}
g->edges = (iflow_edge_t*)realloc(g->edges, (g->num_edges + 1)
* sizeof(iflow_edge_t));
if (g->edges == NULL) {
fprintf(stderr, "Memory error!\n");
return -1;
}
memset(&g->edges[g->num_edges], 0, sizeof(iflow_edge_t));
g->edges[g->num_edges].start_node = start_node;
g->edges[g->num_edges].end_node = end_node;
g->edges[g->num_edges].length = length;
if (add_i_to_a(g->num_edges, &start->num_out_edges, &start->out_edges) != 0) {
return -1;
}
if (add_i_to_a(g->num_edges, &end->num_in_edges, &end->in_edges) != 0) {
return -1;
}
g->num_edges++;
return g->num_edges - 1;
}
static int iflow_graph_add_node(iflow_graph_t *g, int type, int node_type, int obj_class)
{
assert(node_type == IFLOW_SOURCE_NODE || node_type == IFLOW_TARGET_NODE);
/* check for an existing node and update the indexes if not */
if (node_type == IFLOW_SOURCE_NODE) {
if (g->src_index[get_src_index(type)] >= 0)
return g->src_index[get_src_index(type)];
else
g->src_index[type] = g->num_nodes;
} else {
if (g->tgt_index[get_tgt_index(g, type, obj_class)] >= 0) {
return g->tgt_index[get_tgt_index(g, type, obj_class)];
} else {
g->tgt_index[get_tgt_index(g, type, obj_class)] = g->num_nodes;
}
}
/* create a new node */
g->nodes = (iflow_node_t*)realloc(g->nodes, sizeof(iflow_node_t) * (g->num_nodes + 1));
if (!g->nodes) {
fprintf(stderr, "Memory error\n");
return -1;
}
memset(&g->nodes[g->num_nodes], 0, sizeof(iflow_node_t));
g->nodes[g->num_nodes].node_type = node_type;
g->nodes[g->num_nodes].type = type;
g->nodes[g->num_nodes].obj_class = obj_class;
g->num_nodes++;
return g->num_nodes - 1;
}
/* helper for iflow_graph_create */
static int add_edges(iflow_graph_t* g, int obj_class, int rule_idx, bool_t found_read, int read_len,
bool_t found_write, int write_len) {
int i, j, k, ret;
int src_node, tgt_node;
bool_t all_src_types = FALSE;
int cur_src_type;
int num_src_types = 0;
int* src_types = NULL;
bool_t all_tgt_types = FALSE;
int cur_tgt_type;
int num_tgt_types = 0;
int *tgt_types = NULL;
av_item_t* rule;
/* extract all of the rules */
rule = &g->policy->av_access[rule_idx];
ret = extract_types_from_te_rule(rule_idx, RULE_TE_ALLOW, SRC_LIST, &src_types, &num_src_types, g->policy);
if (ret == -1)
return -1;
if (ret == 2)
all_src_types = TRUE;
ret = extract_types_from_te_rule(rule_idx, RULE_TE_ALLOW, TGT_LIST, &tgt_types, &num_tgt_types, g->policy);
if (ret == -1)
return -1;
if (ret == 2)
all_tgt_types = TRUE;
for (i = 0; i < num_src_types; i++) {
if (all_src_types) {
/* skip self */
if (i == 0)
continue;
cur_src_type = i;
} else {
cur_src_type = src_types[i];
}
/* self should never be a src type */
assert(cur_src_type);
if (g->query->num_types) {
bool_t filter_type = FALSE;
for (k = 0; k < g->query->num_types; k++) {
if (g->query->types[k] == cur_src_type) {
filter_type = TRUE;
break;
}
}
if (filter_type) {
continue;
}
}
/* add the source type */
src_node = iflow_graph_add_node(g, cur_src_type, IFLOW_SOURCE_NODE, -1);
if (src_node < 0)
return -1;
for (j = 0; j < num_tgt_types; j++) {
int edge;
if (all_tgt_types) {
cur_tgt_type = j;
if (j == 0)
continue;
} else if (tgt_types[j]==0) {
/* idx 0 is self */
cur_tgt_type = cur_src_type;
} else {
cur_tgt_type = tgt_types[j];
}
if (g->query->num_types) {
bool_t filter_type = FALSE;
for (k = 0; k < g->query->num_types; k++) {
if (g->query->types[k] == cur_tgt_type) {
filter_type = TRUE;
break;
}
}
if (filter_type) {
continue;
}
}
/* add the target type */
tgt_node = iflow_graph_add_node(g, cur_tgt_type, IFLOW_TARGET_NODE, obj_class);
if (tgt_node < 0)
return -1;
if (found_read) {
edge = iflow_graph_connect(g, tgt_node, src_node, read_len);
if (edge < 0) {
fprintf(stderr, "Could not add edge!\n");
return -1;
}
if (add_i_to_a(rule_idx, &g->edges[edge].num_rules,
&g->edges[edge].rules) != 0) {
fprintf(stderr, "Could not add rule!\n");
}
}
if (found_write) {
edge = iflow_graph_connect(g, src_node, tgt_node, write_len);
if (edge < 0) {
fprintf(stderr, "Could not add edge!\n");
return -1;
}
if (add_i_to_a(rule_idx, &g->edges[edge].num_rules,
&g->edges[edge].rules) != 0) {
fprintf(stderr, "Could not add rule!\n");
}
}
}
}
if (!all_src_types) {
free(src_types);
}
if (!all_tgt_types) {
free(tgt_types);
}
return 0;
}
/*
* Create an information flow graph of a policy.
*/
iflow_graph_t *iflow_graph_create(policy_t* policy, iflow_query_t *q)
{
int i, j, k, l, ret;
unsigned char map;
iflow_graph_t* g;
bool_t perm_error = FALSE;
int max_len = PERMMAP_MAX_WEIGHT - q->min_weight + 1;
assert(policy && q);
if (policy->pmap == NULL) {
fprintf(stderr, "Perm map must be loaded first.\n");
return NULL;
}
g = iflow_graph_alloc(policy);
if (g == NULL)
return NULL;
g->query = q;
for (i = 0; i < policy->num_av_access; i++) {
av_item_t* rule;
int cur_obj_class, num_obj_classes = 0, *obj_classes = NULL;
bool_t all_obj_classes = FALSE, all_perms = FALSE;
int cur_perm, num_perms = 0, *perms = NULL;
rule = &policy->av_access[i];
if (rule->type != RULE_TE_ALLOW)
continue;
if (!rule->enabled)
continue;
/* get the object classes for this rule */
ret = extract_obj_classes_from_te_rule(i, RULE_TE_ALLOW, &obj_classes, &num_obj_classes, policy);
if (ret == -1) {
iflow_graph_destroy(g);
return NULL;
} else if (ret == 2) {
all_obj_classes = TRUE;
}
ret = extract_perms_from_te_rule(i, RULE_TE_ALLOW, &perms, &num_perms, policy);
if (ret == -1) {
iflow_graph_destroy(g);
if (!all_obj_classes)
free(obj_classes);
return NULL;
} else if (ret == 2) {
all_perms = TRUE;
}
/* find read or write flows for each object class */
for (j = 0; j < num_obj_classes; j++ ) {
class_perm_map_t* cur_pmap;
bool_t found_read = FALSE, found_write = FALSE;
int cur_obj_options = -1;
int len = 0, read_len, write_len;
if (all_obj_classes)
cur_obj_class = j;
else
cur_obj_class = obj_classes[j];
/* Check to see if we should filter this object class. If we find
* the object class in the obj_options and it doesn't list specific
* perms then we filter. If we find the object class in the obj_options
* but it has specific perms we save the index into obj_options and
* check the perms below */
if (q->num_obj_options != 0) {
bool_t filter_obj_class = FALSE;
for (k = 0; k < q->num_obj_options; k++) {
if (q->obj_options[k].obj_class == cur_obj_class) {
if (q->obj_options[k].num_perms == 0)
filter_obj_class = TRUE;
else
cur_obj_options = k;
break;
}
}
if (filter_obj_class)
continue;
}
cur_pmap = &policy->pmap->maps[cur_obj_class];
if (all_perms) {
ret = get_obj_class_perms(cur_obj_class, &num_perms, &perms, policy);
if (ret != 0) {
iflow_graph_destroy(g);
if (!all_obj_classes)
free(obj_classes);
return NULL;
}
}
read_len = write_len = INT_MAX;
for (k = 0; k < num_perms; k++) {
cur_perm = perms[k];
/* Check to see if we should ignore this permission */
if (cur_obj_options >= 0) {
bool_t filter_perm = FALSE;
for (l = 0; l < q->obj_options[cur_obj_options].num_perms; l++) {
if (q->obj_options[cur_obj_options].perms[l] == cur_perm) {
filter_perm = TRUE;
break;
}
}
if (filter_perm)
continue;
}
/* get the mapping for the perm */
map = 0;
for (l = 0; l < cur_pmap->num_perms; l++) {
if (cur_pmap->perm_maps[l].perm_idx == cur_perm) {
map = cur_pmap->perm_maps[l].map;
len = PERMMAP_MAX_WEIGHT - cur_pmap->perm_maps[l].weight + 1;
if (len < PERMMAP_MIN_WEIGHT)
len = PERMMAP_MIN_WEIGHT;
else if (len > PERMMAP_MAX_WEIGHT)
len = PERMMAP_MAX_WEIGHT;
break;
}
}
if (map == 0) {
perm_error = TRUE;
continue;
}
if (map & PERMMAP_READ) {
if (len < read_len && len <= max_len) {
found_read = TRUE;
read_len = len;
}
}
if (map & PERMMAP_WRITE) {
if (len < write_len && len <= max_len) {
found_write = TRUE;
write_len = len;
}
}
}
if (all_perms)
free(perms);
if (!found_read && !found_write) {
continue;
}
/* if we have found any flows add the edge */
if (add_edges(g, cur_obj_class, i, found_read, read_len, found_write, write_len) != 0) {
iflow_graph_destroy(g);
if (!all_perms)
free(perms);
if (!all_obj_classes)
free(obj_classes);
return NULL;
}
}
if (!all_perms)
free(perms);
if (!all_obj_classes)
free(obj_classes);
}
if (perm_error)
fprintf(stderr, "Not all of the permissions found had associated permission maps.\n");
return g;
}
/* direct information flow */
/* helper for iflow_direct_flows */
static bool_t edge_matches_query(iflow_graph_t* g, iflow_query_t* q, int edge)
{
int end_type, ending_node;
if (g->nodes[g->edges[edge].start_node].type == q->start_type) {
ending_node = g->edges[edge].end_node;
} else {
ending_node = g->edges[edge].start_node;
}
if (q->num_end_types != 0) {
end_type = g->nodes[ending_node].type;
if (find_int_in_array(end_type, q->end_types, q->num_end_types) == -1)
return FALSE;
}
return TRUE;
}
static int iflow_define_flow(iflow_graph_t *g, iflow_t *flow, int direction, int start_node, int edge)
{
int i, end_node, obj_class;
iflow_edge_t *edge_ptr;
edge_ptr = &g->edges[edge];
if (edge_ptr->start_node == start_node) {
end_node = edge_ptr->end_node;
} else {
end_node = edge_ptr->start_node;
}
flow->direction |= direction;
flow->start_type = g->nodes[start_node].type;
flow->end_type = g->nodes[end_node].type;
obj_class = g->nodes[edge_ptr->start_node].obj_class;
if (obj_class == -1)
obj_class = g->nodes[edge_ptr->end_node].obj_class;
for (i = 0; i < edge_ptr->num_rules; i++) {
if (find_int_in_array(edge_ptr->rules[i], flow->obj_classes[obj_class].rules,
flow->obj_classes[obj_class].num_rules) == -1) {
if (add_i_to_a(edge_ptr->rules[i], &flow->obj_classes[obj_class].num_rules,
&flow->obj_classes[obj_class].rules) < 0) {
return -1;
}
}
}
return 0;
}
static int direct_find_flow(iflow_graph_t *g, int start_node, int end_node, int *num_answers, iflow_t **answers)
{
iflow_t *cur;
int i;
assert(num_answers);
/* see if a flow already exists */
if (*answers) {
for (i = 0; i < *num_answers; i++) {
cur = &(*answers)[i];
if (cur->start_type == g->nodes[start_node].type &&
cur->end_type == g->nodes[end_node].type) {
return i;
}
}
}
/* if we didn't find a matching flow make space for a new one */
*answers = (iflow_t*)realloc(*answers, (*num_answers + 1)
* sizeof(iflow_t));
if (*answers == NULL) {
fprintf(stderr, "Memory error!\n");
return -1;
}
if (iflow_init(g, &(*answers)[*num_answers])) {
return -1;
}
(*num_answers)++;
return *num_answers - 1;
}
int iflow_direct_flows(policy_t *policy, iflow_query_t *q, int *num_answers,
iflow_t **answers)
{
int i, j, edge, ret = 0;
iflow_node_t* node;
bool_t edge_matches;
int num_nodes, *nodes;
int flow, end_node;
iflow_graph_t *g;
if (!iflow_query_is_valid(q, policy))
return -1;
g = iflow_graph_create(policy, q);
if (!g) {
fprintf(stderr, "Error creating graph\n");
return -1;
}
*num_answers = 0;
*answers = NULL;
if (iflow_graph_get_nodes_for_type(g, q->start_type, &num_nodes, &nodes) < 0)
return -1;
/*
* Because the graph doesn't contain every type (i.e. it is possible that the query
* made a type not match), not finding a node means that there are no flows. This
* used to indicate an error.
*/
if (num_nodes == 0) {
return 0;
}
if (q->direction == IFLOW_IN || q->direction == IFLOW_EITHER || q->direction == IFLOW_BOTH) {
for (i = 0; i < num_nodes; i++) {
node = &g->nodes[nodes[i]];
for (j = 0; j < node->num_in_edges; j++) {
edge = node->in_edges[j];
edge_matches = edge_matches_query(g, q, edge);
if (!edge_matches)
continue;
if (g->edges[edge].start_node == nodes[i])
end_node = g->edges[edge].end_node;
else
end_node = g->edges[edge].start_node;
flow = direct_find_flow(g, nodes[i], end_node, num_answers, answers);
if (flow < 0) {
ret = -1;
goto out;
}
if (iflow_define_flow(g, &(*answers)[flow], IFLOW_IN, nodes[i], edge)) {
ret = -1;
goto out;
}
}
}
}
if (q->direction == IFLOW_OUT || q->direction == IFLOW_EITHER || q->direction == IFLOW_BOTH) {
for (i = 0; i < num_nodes; i++) {
node = &g->nodes[nodes[i]];
for (j = 0; j < node->num_out_edges; j++) {
edge = node->out_edges[j];
edge_matches = edge_matches_query(g, q, edge);
if (!edge_matches)
continue;
if (g->edges[edge].start_node == nodes[i])
end_node = g->edges[edge].end_node;
else
end_node = g->edges[edge].start_node;
flow = direct_find_flow(g, nodes[i], end_node, num_answers, answers);
if (flow < 0) {
ret = -1;
goto out;
}
if (iflow_define_flow(g, &(*answers)[flow], IFLOW_OUT, nodes[i], edge)) {
ret = -1;
goto out;
}
}
}
}
if (*num_answers == 0)
goto out;
/* do some extra checks for both */
if (q->direction == IFLOW_BOTH) {
int tmp_num_answers = *num_answers;
iflow_t *tmp_answers = *answers;
*num_answers = 0;
*answers = NULL;
for (i = 0; i < tmp_num_answers; i++) {
if (tmp_answers[i].direction != IFLOW_BOTH) {
iflow_destroy_data(&tmp_answers[i]);
continue;
}
*answers = (iflow_t*)realloc(*answers, (*num_answers + 1)
* sizeof(iflow_t));
if (*answers == NULL) {
fprintf(stderr, "Memory error!\n");
goto out;
}
(*answers)[*num_answers] = tmp_answers[i];
*num_answers += 1;
}
free(tmp_answers);
}
out:
if (nodes)
free(nodes);
iflow_graph_destroy(g);
return ret;
}
static int ta_find_edge(iflow_graph_t *g, iflow_query_t *q, int path_len, int *path, int start)
{
int i, edge = -1;
if (q->direction == IFLOW_OUT) {
for (i = 0; i < g->nodes[path[start]].num_out_edges; i++) {
edge = g->nodes[path[start]].out_edges[i];
if (g->edges[edge].start_node == path[start] &&
g->edges[edge].end_node == path[start + 1])
break;
}
if (i == g->nodes[path[start]].num_out_edges) {
fprintf(stderr, "Did not find an edge\n");
return -1;
}
} else {
for (i = 0; i < g->nodes[path[start]].num_in_edges; i++) {
edge = g->nodes[path[start]].in_edges[i];
if (g->edges[edge].end_node == path[start] &&
g->edges[edge].start_node == path[start + 1])
break;
}
if (i == g->nodes[path[start]].num_in_edges) {
fprintf(stderr, "Did not find an edge\n");
return -1;
}
}
return edge;
}
static iflow_path_t *ta_build_path(iflow_graph_t *g, iflow_query_t *q, int path_len, int *path)
{
int i, length, edge;
iflow_path_t *p;
p = (iflow_path_t*)malloc(sizeof(iflow_path_t));
if (!p) {
fprintf(stderr, "Memory error\n");
return NULL;
}
memset(p, 0, sizeof(iflow_path_t));
p->iflows = (iflow_t*)malloc(sizeof(iflow_t) * (path_len - 1));
if (!p->iflows) {
fprintf(stderr, "Memory error\n");
return NULL;
}
p->num_iflows = path_len - 1;
memset(p->iflows, 0, sizeof(iflow_t) * (path_len - 1));
/* build the path */
length = 0;
for (i = 0; i < path_len - 1; i++) {
edge = ta_find_edge(g, q, path_len, path, i);
if (edge < 0)
return NULL;
length += g->edges[edge].length;
if (iflow_init(g, &p->iflows[i])) {
fprintf(stderr, "Memory error\n");
return NULL;
}
if (q->direction == IFLOW_OUT) {
if (iflow_define_flow(g, &p->iflows[i], IFLOW_OUT,
path[i], edge))
return NULL;
} else {
if (iflow_define_flow(g, &p->iflows[i], IFLOW_IN,
path[i + 1], edge))
return NULL;
}
}
p->length = length;
return p;
}
static bool_t ta_iflow_equal(iflow_t *a, iflow_t *b)
{
if (a->start_type != b->start_type || a->end_type != b->end_type || a->direction != b->direction)
return FALSE;
return TRUE;
}
/* helper for iflow_transitive_flows */
static int transitive_answer_append(iflow_graph_t *g, iflow_query_t *q, iflow_transitive_t* a,
int end_node, int path_len, int *path)
{
int i, j, cur_type, cur;
iflow_path_t *p, *last_path = NULL;
bool_t found_dup, new_path = FALSE;
p = ta_build_path(g, q, path_len, path);
if (!p)
return -1;
/* First we look for duplicate paths */
cur_type = g->nodes[end_node].type;
for (i = 0; i < a->num_end_types; i++) {
if (a->end_types[i] != cur_type)
continue;
/* find the last path while checking for duplicates */
last_path = a->paths[i];
while (1) {
if (last_path->num_iflows != p->num_iflows)
goto next;
found_dup = TRUE;
for (j = 0; j < last_path->num_iflows; j++) {
if (!ta_iflow_equal(&last_path->iflows[j], &p->iflows[j])) {
found_dup = FALSE;
break;
}
}
/* found a dup TODO - make certain all of the object class / rules are kept */
if (found_dup) {
iflow_path_destroy(p);
return 0;
}
next:
if (!last_path->next)
break;
last_path = last_path->next;
}
new_path = TRUE;
a->num_paths[i]++;
last_path->next = p;
break;
}
/* If we are here there are no other paths with this end type */
if (!last_path) {
new_path = TRUE;
cur = a->num_end_types;
if (add_i_to_a(cur_type, &a->num_end_types, &a->end_types))
return -1;
a->paths = (iflow_path_t**)realloc(a->paths, a->num_end_types
* sizeof(iflow_path_t*));
if (a->paths == NULL) {
fprintf(stderr, "Memory error!\n");
return -1;
}
a->num_paths = (int*)realloc(a->num_paths, a->num_end_types
* sizeof(int));
if (a->num_paths == NULL) {
fprintf(stderr, "Memory error!\n");
return -1;
}
new_path = TRUE;
a->paths[cur] = p;
a->num_paths[cur] = 1;
}
if (new_path)
return 1;
return 0;
}
static int iflow_path_compare(const void *a, const void *b)
{
iflow_path_t *path_a, *path_b;
path_a = *((iflow_path_t**)a);
path_b = *((iflow_path_t**)b);
if (path_a->length == path_b->length)
return 0;
else if (path_a->length < path_b->length)
return -1;
else
return 1;
}
static iflow_path_t *iflow_sort_paths(iflow_path_t *path)
{
int i, num_paths;
iflow_path_t *cur, *start, **paths;
if (!path) {
fprintf(stderr, "sort_iflow_paths got NULL path\n");
return NULL;
}
num_paths = 0;
cur = path;
while (cur) {
num_paths++;
cur = cur->next;
}
if (num_paths == 1) {
return path;
}
paths = (iflow_path_t**)malloc(sizeof(iflow_path_t*) * num_paths);
if (!paths) {
fprintf(stderr, "Memory error\n");
return NULL;
}
memset(paths, 0, sizeof(iflow_path_t*) * num_paths);
i = 0;
cur = path;
while (cur) {
paths[i++] = cur;
cur = cur->next;
}
qsort(paths, num_paths, sizeof(iflow_path_t*), iflow_path_compare);
cur = start = paths[0];
for (i = 1; i < num_paths; i++) {
cur->next = paths[i];
cur = cur->next;
}
cur->next = NULL;
return start;
}
static int shortest_path_find_path(iflow_graph_t *g, int start_node, int end_node, int *path)
{
int next_node = end_node;
int i, tmp, path_len = 0;
while (1) {
path[path_len++] = next_node;
if (next_node == start_node)
break;
if (path_len >= g->num_nodes) {
fprintf(stderr, "Infinite loop in shortest_path_find_path\n");
return -1;
}
next_node = g->nodes[next_node].parent;
if (next_node >= g->num_nodes) {
fprintf(stderr, "Something strange in shortest_path_find_path\n");
return -1;
}
}
/* reverse the path */
for (i = 0; i < path_len / 2; i++) {
tmp = path[i];
path[i] = path[(path_len - 1) - i];
path[(path_len - 1) - i] = tmp;
}
return path_len;
}
/* This is a label correcting shortest path algorithm
* see Bertsekas, D. P., "A Simple and Fast Label Correcting Algorithm for Shortest Paths,"
* Networks, Vol. 23, pp. 703-709, 1993. for more information. A label correcting algorithm is
* needed instead of the more common Dijkstra label setting algorithm to correctly handle the
* the cycles that are possible in these graphs.
*
* This algorithm finds the shortest path between a given start node and all other nodes in
* the graph. Any paths that it finds it appends to the iflow_transitive_t structure. This
* is a basic label correcting algorithm with 1 optimization. It uses the D'Esopo-Pape method
* for node selection in the node queue. Why is this faster? The paper referenced above says
* "No definitive explanation has been given." They have fancy graphs to show that it is faster
* though and the important part is that the worst case isn't much worse that N^2 - much better
* than an n^3 transitive closure. Additionally, most normal sparse graphs are significantly better
* than the worst case.
*/
int iflow_graph_shortest_path(iflow_graph_t *g, int start_node, iflow_transitive_t *a, iflow_query_t *q)
{
int i, rc = 0;
int *path = NULL;
queue_t queue = NULL;
queue = queue_create();
if (!queue) {
fprintf(stderr, "Error creating queue\n");
rc = -1;
goto out;
}
path = (int*)malloc(g->num_nodes * sizeof(int));
if (!path) {
rc = -1;
goto out;
}
/* initialization */
g->nodes[start_node].distance = 0;
g->nodes[start_node].parent = -1;
g->nodes[start_node].color = IFLOW_COLOR_RED;
for (i = 0; i < g->num_nodes; i++) {
if (i == start_node)
continue;
g->nodes[i].distance = INT_MAX;
g->nodes[i].parent = -1;
g->nodes[i].color = IFLOW_COLOR_WHITE;
}
if (queue_insert(queue, (void*)(start_node + 1)) < 0) {
fprintf(stderr, "Error inserting into queue\n");
rc = -1;
goto out;
}
while (queue_head(queue)) {
void *cur_ptr;
int cur;
int num_edges;
cur_ptr = queue_remove(queue);
if (cur_ptr == NULL) {
rc = -1;
goto out;
}
cur = ((int)cur_ptr) - 1;
g->nodes[cur].color = IFLOW_COLOR_GREY;
if (q->direction == IFLOW_OUT)
num_edges = g->nodes[cur].num_out_edges;
else
num_edges = g->nodes[cur].num_in_edges;
for (i = 0; i < num_edges; i++) {
int edge, node;
if (q->direction == IFLOW_OUT) {
edge = g->nodes[cur].out_edges[i];
node = g->edges[edge].end_node;
} else {
edge = g->nodes[cur].in_edges[i];
node = g->edges[edge].start_node;
}
if (start_node == node)
continue;
if (g->nodes[node].distance > (g->nodes[cur].distance + g->edges[edge].length)) {
g->nodes[node].distance = g->nodes[cur].distance + g->edges[edge].length;
g->nodes[node].parent = cur;
if (g->nodes[node].color != IFLOW_COLOR_RED) {
/* If this node has been inserted into the queue before insert
* it at the beginning, otherwise it goes to the end. See the
* comment at the beginning of the function for why. */
if (g->nodes[node].color == IFLOW_COLOR_GREY) {
if (queue_push(queue, (void*)(node + 1)) < 0) {
fprintf(stderr, "Error inserting into queue\n");
rc = -1;
goto out;
}
} else {
if (queue_insert(queue, (void*)(node + 1)) < 0) {
fprintf(stderr, "Error inserting into queue\n");
rc = -1;
goto out;
}
}
g->nodes[node].color = IFLOW_COLOR_RED;
}
}
}
}
/* Find all of the paths and stick them in the iflow_transitive_t struct */
for (i = 0; i < g->num_nodes; i++) {
int path_len;
if (g->nodes[i].parent == -1)
continue;
if (i == start_node)
continue;
if (q->num_end_types) {
if (find_int_in_array(g->nodes[i].type, q->end_types, q->num_end_types) == -1) {
continue;
}
}
path_len = shortest_path_find_path(g, start_node, i, path);
if (path_len < 0) {
rc = -1;
goto out;
}
if (transitive_answer_append(g, q, a, i, path_len, path) == -1) {
rc = -1;
goto out;
}
}
out:
if (queue)
queue_destroy(queue);
if (path)
free(path);
return rc;
}
iflow_transitive_t *iflow_transitive_flows(policy_t *policy, iflow_query_t *q)
{
int num_nodes, *nodes;
int i;
iflow_transitive_t *a;
iflow_graph_t *g;
if (!iflow_query_is_valid(q, policy))
return NULL;
if (!((q->direction == IFLOW_OUT ) || (q->direction == IFLOW_IN))) {
fprintf(stderr, "Direction must be IFLOW_IN or IFLOW_OUT\n");
return NULL;
}
g = iflow_graph_create(policy, q);
if (!g) {
fprintf(stderr, "Error creating graph\n");
return NULL;
}
a = (iflow_transitive_t*)malloc(sizeof(iflow_transitive_t));
if (a == NULL) {
fprintf(stderr, "Memory error!\n");
goto err;
}
memset(a, 0, sizeof(iflow_transitive_t));
if (iflow_graph_get_nodes_for_type(g, q->start_type, &num_nodes, &nodes) < 0)
return NULL;
if (num_nodes == 0) {
goto out;
}
a->start_type = q->start_type;
for (i = 0; i < num_nodes; i++) {
if (iflow_graph_shortest_path(g, nodes[i], a, q) != 0)
goto err;
}
/* sort the paths by length */
for (i = 0; i < a->num_end_types; i++) {
/* sort the paths by length */
a->paths[i] = iflow_sort_paths(a->paths[i]);
if (a->paths[i] == NULL) {
goto err;
}
}
out:
iflow_graph_destroy(g);
free(g);
if (nodes)
free(nodes);
return a;
err:
iflow_transitive_destroy(a);
a = NULL;
goto out;
}
/* Random shuffle from Knuth Seminumerical Algorithms p. 139 */
static void shuffle_list(int len, int *list)
{
float U;
int j, k, tmp;
srand((int)time(NULL));
for (j = len - 1; j > 0; j--) {
/* get a random number between 1 and j */
U = rand() / (float)RAND_MAX;
k = ((int)(j * U)) + 1;
tmp = list[k];
list[k] = list[j];
list[j] = tmp;
}
}
static int get_random_edge_list(int edges_len, int **edge_list)
{
int i;
*edge_list = (int*)malloc(sizeof(int) * edges_len);
if (!*edge_list) {
fprintf(stderr, "Memory error\n");
return -1;
}
for (i = 0; i < edges_len; i++)
(*edge_list)[i] = i;
shuffle_list(edges_len, *edge_list);
return 0;
}
typedef struct bfs_random_state {
iflow_graph_t *g;
queue_t queue;
iflow_query_t *q;
policy_t *policy;
iflow_transitive_t *a;
int *path;
int num_nodes;
int *nodes;
int num_enodes;
int *enodes;
int cur;
} bfs_random_state_t;
void bfs_random_state_destroy(bfs_random_state_t *s)
{
if (s->g) {
iflow_graph_destroy(s->g);
free(s->g);
}
if (s->q)
iflow_query_destroy(s->q);
if (s->queue) {
queue_destroy(s->queue);
}
if (s->path)
free(s->path);
if (s->nodes)
free(s->nodes);
if (s->enodes)
free(s->enodes);
}
int bfs_random_state_init(bfs_random_state_t *s, policy_t *p, iflow_query_t *q, iflow_transitive_t *a)
{
assert(s);
memset(s, 0, sizeof(bfs_random_state_t));
s->policy = p;
s->a = a;
s->q = iflow_query_create();
if (!s->q) {
fprintf(stderr, "Error creating query\n");
return -1;
}
if (iflow_query_copy(s->q, q)) {
fprintf(stderr, "Error copy query\n");
return -1;
}
if (!iflow_query_is_valid(q, p))
return -1;
if (q->num_end_types != 1) {
fprintf(stderr, "You must provide exactly 1 end type\n");
return -1;
}
s->g = iflow_graph_create(p, q);
if (!s->g) {
fprintf(stderr, "Error creating graph\n");
return -1;
}
s->queue = queue_create();
if (!s->queue) {
fprintf(stderr, "Error creating queue\n");
goto err;
}
if (iflow_graph_get_nodes_for_type(s->g, q->start_type, &s->num_nodes, &s->nodes) < 0)
goto err;
if (iflow_graph_get_nodes_for_type(s->g, q->end_types[0], &s->num_enodes, &s->enodes) <0)
goto err;
s->path = (int*)malloc(sizeof(int) * s->g->num_nodes);
if (!s->path) {
fprintf(stderr, "Memory error\n");
goto err;
}
return 0;
err:
bfs_random_state_destroy(s);
return -1;
}
static int breadth_first_find_path(iflow_graph_t *g, int node, int *path)
{
int next_node = node;
int path_len = g->nodes[node].distance + 1;
int i = path_len - 1;
while (i >= 0) {
path[i] = next_node;
next_node = g->nodes[next_node].parent;
i--;
}
return path_len;
}
static int do_breadth_first_search_random(bfs_random_state_t *s)
{
int i, ret = 0, path_len, *edge_list = NULL;
int num_edges, cur;
void *cur_ptr;
bool_t found_new_path = FALSE;
while (queue_head(s->queue)) {
cur_ptr = queue_remove(s->queue);
if (cur_ptr == NULL) {
ret = -1;
goto out;
}
cur = ((int)cur_ptr) - 1;
if (find_int_in_array(cur, s->enodes, s->num_enodes) != -1) {
path_len = breadth_first_find_path(s->g, cur, s->path);
if (path_len == -1) {
ret = -1;
goto out;
}
ret = transitive_answer_append(s->g, s->q, s->a, cur, path_len, s->path);
if (ret == -1) {
fprintf(stderr, "Error in transitive answer append\n");
goto out;
} else if (ret > 0) {
found_new_path = TRUE;
}
}
s->g->nodes[cur].color = IFLOW_COLOR_BLACK;
if (s->q->direction == IFLOW_OUT)
num_edges = s->g->nodes[cur].num_out_edges;
else
num_edges = s->g->nodes[cur].num_in_edges;
if (num_edges) {
if (get_random_edge_list(num_edges, &edge_list) < 0) {
ret = -1;
goto out;
}
}
for (i = 0; i < num_edges; i++) {
int cur_edge, cur_node;
if (s->q->direction == IFLOW_OUT) {
cur_edge = s->g->nodes[cur].out_edges[edge_list[i]];
cur_node = s->g->edges[cur_edge].end_node;
} else {
cur_edge = s->g->nodes[cur].in_edges[edge_list[i]];
cur_node = s->g->edges[cur_edge].start_node;
}
if (s->g->nodes[cur_node].color == IFLOW_COLOR_WHITE) {
s->g->nodes[cur_node].color = IFLOW_COLOR_GREY;
s->g->nodes[cur_node].distance = s->g->nodes[cur].distance + 1;
s->g->nodes[cur_node].parent = cur;
if (queue_insert(s->queue, (void*)(cur_node + 1)) < 0) {
fprintf(stderr, "Error inserting into queue\n");
ret = -1;
goto out;
}
}
}
if (edge_list) {
free(edge_list);
edge_list = NULL;
}
}
if (found_new_path)
ret = 1;
out:
if (edge_list)
free(edge_list);
return ret;
}
int iflow_find_paths_next(void *state)
{
int j, start_node;
bfs_random_state_t *s = (bfs_random_state_t*)state;
int num_paths;
/* paint all nodes white */
for (j = 0; j < s->g->num_nodes; j++) {
s->g->nodes[j].color = IFLOW_COLOR_WHITE;
s->g->nodes[j].parent = -1;
s->g->nodes[j].distance = -1;
}
start_node = s->nodes[s->cur];
s->g->nodes[start_node].color = IFLOW_COLOR_GREY;
s->g->nodes[start_node].distance = 0;
s->g->nodes[start_node].parent = -1;
if (queue_insert(s->queue, (void*)(start_node + 1)) < 0) {
fprintf(stderr, "Error inserting into queue\n");
return -1;
}
if (do_breadth_first_search_random(s) < 0)
return -1;
s->cur++;
if (s->cur >= s->num_nodes) {
s->cur = 0;
shuffle_list(s->num_nodes, s->nodes);
}
if (s->a->num_paths)
num_paths = s->a->num_paths[0];
else
num_paths = 0;
return num_paths;
}
/* caller does not need to free the query */
void *iflow_find_paths_start(policy_t *policy, iflow_query_t *q)
{
bfs_random_state_t *s;
iflow_transitive_t *a;
s = (bfs_random_state_t*)malloc(sizeof(bfs_random_state_t));
if (!s) {
fprintf(stderr, "Memory error\n");
return NULL;
}
a = (iflow_transitive_t*)malloc(sizeof(iflow_transitive_t));
if (!a) {
free(s);
fprintf(stderr, "Memory error\n");
return NULL;
}
memset(a, 0, sizeof(iflow_transitive_t));
if (bfs_random_state_init(s, policy, q, a)) {
fprintf(stderr, "Random state init error\n");
free(s);
free(a);
return NULL;
}
return (void*)s;
}
iflow_transitive_t *iflow_find_paths_end(void *state)
{
bfs_random_state_t *s = (bfs_random_state_t*)state;
iflow_transitive_t *a;
int i;
a = s->a;
bfs_random_state_destroy(s);
free(s);
/* sort the paths by length */
for (i = 0; i < a->num_end_types; i++) {
/* sort the paths by length */
a->paths[i] = iflow_sort_paths(a->paths[i]);
if (a->paths[i] == NULL) {
return NULL;
}
}
return a;
}
void iflow_find_paths_abort(void *state)
{
bfs_random_state_t *s = (bfs_random_state_t*)state;
bfs_random_state_destroy(s);
free(s);
iflow_transitive_destroy(s->a);
}
|