~mathiaz/apparmor/ubuntu-mathiaz

« back to all changes in this revision

Viewing changes to parser/libapparmor_re/regexp.y

  • Committer: Mathias Gug
  • Date: 2008-02-04 18:57:00 UTC
  • mfrom: (885.1.5 apparmor)
  • Revision ID: mathiaz@ubuntu.com-20080204185700-wwlyq0ksssxclv8w
Merge  ubuntu branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
852
852
    }
853
853
 
854
854
    uint32_t flag;
855
 
};
 
855
 };
856
856
 
857
857
class ExactMatchFlag : public MatchFlag {
858
858
public:
871
871
    for (States::iterator i = states.begin(); i != states.end(); i++) {
872
872
        uint32_t accept = accept_perms(*i);
873
873
        if (*i == start || accept) {
874
 
            if ((accept & AA_EXEC_MODIFIERS) &&
875
 
                !AA_EXEC_SINGLE_MODIFIER_SET(accept))
876
 
                return *i;
 
874
            if (accept & AA_ERROR_BIT)
 
875
                    return *i;
877
876
        }
878
877
    }
879
878
    return NULL;
891
890
            if (*i == start)
892
891
                os << " <==";
893
892
            if (accept) {
894
 
                os << " (" << accept << ')';
 
893
                os << " (0x" << hex << accept << dec << ')';
895
894
            }
896
895
            os << endl;
897
896
        }
1493
1492
    }
1494
1493
}
1495
1494
 
1496
 
#define ACCUMULATING_FLAGS \
1497
 
        (AA_MAY_READ | AA_MAY_WRITE | AA_MAY_APPEND | AA_MAY_EXEC | \
1498
 
         AA_MAY_LINK | AA_MAY_LOCK | AA_EXEC_MMAP | AA_CHANGE_PROFILE)
 
1495
static inline int diff_qualifiers(uint32_t perm1, uint32_t perm2)
 
1496
{
 
1497
        return ((perm1 & AA_EXEC_MODIFIERS) && (perm2 & AA_EXEC_MODIFIERS) &&
 
1498
                (perm1 & AA_EXEC_MODIFIERS) != (perm2 & AA_EXEC_MODIFIERS));
 
1499
}
1499
1500
 
1500
1501
/**
1501
1502
 * Compute the permission flags that this state corresponds to. If we
1507
1508
    uint32_t perms = 0, exact_match_perms = 0;
1508
1509
 
1509
1510
    for (State::iterator i = state->begin(); i != state->end(); i++) {
1510
 
        if (MatchFlag *match = dynamic_cast<MatchFlag *>(*i)) {
1511
 
                perms |= match->flag;
1512
 
                if (dynamic_cast<ExactMatchFlag *>(match))
1513
 
                        exact_match_perms |= match->flag;
1514
 
        }
 
1511
            MatchFlag *match;
 
1512
            if (!(match= dynamic_cast<MatchFlag *>(*i)))
 
1513
                continue;
 
1514
            if (dynamic_cast<ExactMatchFlag *>(match)) {
 
1515
                    if (!is_merged_x_consistent(exact_match_perms,
 
1516
                                                match->flag))
 
1517
                            exact_match_perms |= AA_ERROR_BIT;
 
1518
                    exact_match_perms |= match->flag;
 
1519
            } else {
 
1520
                    if (!is_merged_x_consistent(perms, match->flag))
 
1521
                            perms |= AA_ERROR_BIT;
 
1522
                    perms |= match->flag;
 
1523
            }
1515
1524
    }
1516
1525
 
1517
 
    if (exact_match_perms & ~ACCUMULATING_FLAGS)
1518
 
        perms = (exact_match_perms & ~ACCUMULATING_FLAGS) |
1519
 
                (perms & ACCUMULATING_FLAGS);
1520
 
 
 
1526
    perms |= exact_match_perms &
 
1527
            ~(AA_USER_EXEC_TYPE | AA_OTHER_EXEC_TYPE);
 
1528
 
 
1529
    if (exact_match_perms & AA_USER_EXEC_TYPE)
 
1530
            perms = (exact_match_perms & AA_USER_EXEC_TYPE) |
 
1531
                    (perms & ~AA_USER_EXEC_TYPE);
 
1532
 
 
1533
    if (exact_match_perms & AA_OTHER_EXEC_TYPE)
 
1534
            perms = (exact_match_perms & AA_OTHER_EXEC_TYPE) |
 
1535
                    (perms & ~AA_OTHER_EXEC_TYPE);
 
1536
 
 
1537
 if (perms & AA_ERROR_BIT) {
 
1538
     fprintf(stderr, "error bit 0x%x\n", perms);
 
1539
     exit(255);
 
1540
}
 
1541
 /*
 
1542
     if (perms & ~AA_VALID_PERMS)
 
1543
        yyerror(_("Internal error accumulated invalid perm 0x%llx\n"), perms);
 
1544
 */
1521
1545
    return perms;
1522
1546
}
1523
1547
 
1524
1548
extern "C" int aare_add_rule(aare_ruleset_t *rules, char *rule, uint32_t perms)
1525
1549
{
1526
 
    static MatchFlag *match_flags[sizeof(perms) * 8];
1527
 
    static ExactMatchFlag *exact_match_flags[sizeof(perms) * 8];
 
1550
    static MatchFlag *match_flags[sizeof(perms) * 8 - 1];
 
1551
    static MatchFlag *exec_match_flags[8 * 2];
 
1552
    static ExactMatchFlag *exact_match_flags[8 * 2];
1528
1553
    Node *tree, *accept;
1529
1554
    int exact_match;
1530
1555
 
1551
1576
    if (rules->reverse)
1552
1577
        flip_tree(tree);
1553
1578
 
 
1579
#define ALL_EXEC_TYPE (AA_USER_EXEC_TYPE | AA_OTHER_EXEC_TYPE)
 
1580
#define EXTRACT_X_INDEX(perm, shift) (((perm) >> (shift + 7)) & 0x7)
 
1581
 
 
1582
if (perms & ALL_EXEC_TYPE && (!perms & AA_EXEC_BITS))
 
1583
        fprintf(stderr, "adding X rule without MAY_EXEC: 0x%x %s\n", perms, rule);
 
1584
 
1554
1585
    accept = NULL;
1555
 
    for (unsigned int n = 0; perms && n < sizeof(perms) * 8; n++) {
 
1586
    for (unsigned int n = 0; perms && n < (sizeof(perms) * 8) - 1; n++) {
1556
1587
        uint32_t mask = 1 << n;
1557
1588
 
1558
1589
        if (perms & mask) {
1559
1590
            perms &= ~mask;
1560
1591
 
1561
1592
            Node *flag;
1562
 
            if (exact_match && (mask & ~ACCUMULATING_FLAGS)) {
1563
 
                    if (exact_match_flags[n])
1564
 
                        flag = exact_match_flags[n]->dup();
1565
 
                    else {
1566
 
                        exact_match_flags[n] = new ExactMatchFlag(mask);
1567
 
                        flag = exact_match_flags[n];
1568
 
                    }
 
1593
            if (mask & AA_EXEC_BITS) {
 
1594
                    uint32_t eperm = 0;
 
1595
                    uint32_t index = 0;
 
1596
                    if (mask & AA_USER_EXEC_TYPE) {
 
1597
                            eperm = mask | (perms & AA_USER_EXEC_TYPE);
 
1598
                            index = EXTRACT_X_INDEX(eperm, AA_USER_SHIFT);
 
1599
                    } else {
 
1600
                            eperm = mask | (perms & AA_OTHER_EXEC_TYPE);
 
1601
                            index = EXTRACT_X_INDEX(eperm, AA_OTHER_SHIFT) + 8;
 
1602
                    }
 
1603
                    if (exact_match) {
 
1604
                            if (exact_match_flags[index]) {
 
1605
                                    flag = exact_match_flags[index]->dup();
 
1606
                            } else {
 
1607
                                exact_match_flags[index] = new ExactMatchFlag(eperm);
 
1608
                                    flag = exact_match_flags[index];
 
1609
                            }
 
1610
                    } else {
 
1611
                            if (exec_match_flags[index]) {
 
1612
                                    flag = exec_match_flags[index]->dup();
 
1613
                            } else {
 
1614
                                exec_match_flags[index] = new MatchFlag(eperm);
 
1615
                                    flag = exec_match_flags[index];
 
1616
                            }
 
1617
                    }
 
1618
            } else if (mask & ALL_EXEC_TYPE) {
 
1619
                    /* these cases are covered by EXEC_BITS */
 
1620
                    continue;
1569
1621
            } else {
1570
 
                    if (match_flags[n])
 
1622
                    if (match_flags[n]) {
1571
1623
                        flag = match_flags[n]->dup();
1572
 
                    else {
 
1624
                    } else {
1573
1625
                        match_flags[n] = new MatchFlag(mask);
1574
1626
                        flag = match_flags[n];
1575
1627
                    }
1580
1632
                    accept = flag;
1581
1633
        }
1582
1634
    }
 
1635
 
1583
1636
    rules->root = new AltNode(rules->root, new CatNode(tree, accept));
1584
1637
 
1585
1638
    return 1;
 
1639
 
1586
1640
}
1587
1641
 
1588
 
#undef ACCUMULATING_FLAGS
1589
 
 
1590
1642
/* create a dfa from the ruleset
1591
1643
 * returns: buffer contain dfa tables, @size set to the size of the tables
1592
1644
 *          else NULL on failure
1598
1650
 
1599
1651
    label_nodes(rules->root);
1600
1652
    DFA dfa(rules->root);
 
1653
 
1601
1654
    map<uchar, uchar> eq;
1602
1655
    if (equiv_classes) {
1603
1656
        eq = dfa.equivalence_classes();