~ubuntu-branches/ubuntu/raring/apparmor/raring

« back to all changes in this revision

Viewing changes to parser/parser_merge.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-03-23 16:42:01 UTC
  • Revision ID: james.westby@ubuntu.com-20070323164201-jkax6f0oku087b7l
Tags: upstream-2.0.1+510.dfsg
ImportĀ upstreamĀ versionĀ 2.0.1+510.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: parser_merge.c 505 2007-03-30 15:59:13Z agruen $ */
 
2
 
 
3
/*
 
4
 *   Copyright (c) 1999, 2000, 2003, 2004, 2005 NOVELL (All rights reserved)
 
5
 *
 
6
 *   This program is free software; you can redistribute it and/or
 
7
 *   modify it under the terms of version 2 of the GNU General Public
 
8
 *   License published by the Free Software Foundation.
 
9
 *
 
10
 *   This program is distributed in the hope that it will be useful,
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *   GNU General Public License for more details.
 
14
 *
 
15
 *   You should have received a copy of the GNU General Public License
 
16
 *   along with this program; if not, contact Novell, Inc.
 
17
 */
 
18
 
 
19
#include <linux/unistd.h>
 
20
 
 
21
#include <stdio.h>
 
22
#include <string.h>
 
23
#include <stdlib.h>
 
24
#include <errno.h>
 
25
#include <libintl.h>
 
26
#define _(s) gettext(s)
 
27
 
 
28
#include "parser.h"
 
29
 
 
30
 
 
31
static inline int count_net_entries(struct codomain *cod)
 
32
{
 
33
        struct cod_net_entry *list;
 
34
        int count = 0;
 
35
        for (list = cod->net_entries; list; list = list->next)
 
36
                count++;
 
37
        return count;
 
38
}
 
39
 
 
40
static int file_comp(const void *c1, const void *c2)
 
41
{
 
42
        struct cod_entry **e1, **e2;
 
43
        e1 = (struct cod_entry **)c1;
 
44
        e2 = (struct cod_entry **)c2;
 
45
        //PERROR("strcmp %s %s\n", (*e1)->name, (*e2)->name);
 
46
        return strcmp((*e1)->name, (*e2)->name);
 
47
}
 
48
 
 
49
static int process_file_entries(struct codomain *cod)
 
50
{
 
51
        int n, count;
 
52
        struct cod_entry *flist, *cur, *next;
 
53
        struct cod_entry **table;
 
54
 
 
55
        for (flist = cod->entries, n = 0; flist; flist = flist->next)
 
56
                n++;
 
57
 
 
58
        count = n;
 
59
        if (count < 2)
 
60
                return 1;
 
61
 
 
62
        table = malloc(sizeof(struct cod_entry *) * (count + 1));
 
63
        if (!table) {
 
64
                PERROR(_("Couldn't merge entries. Out of Memory\n"));
 
65
                return 0;
 
66
        }
 
67
 
 
68
        n = 0;
 
69
        for (flist = cod->entries; flist; flist = flist->next) {
 
70
                table[n] = flist;
 
71
                n++;
 
72
        }
 
73
 
 
74
        qsort(table, count, sizeof(struct cod_entry *), file_comp);
 
75
        table[count] = NULL;
 
76
 
 
77
#define CHECK_CONFLICT_UNSAFE(a, b) \
 
78
        ((HAS_EXEC_UNSAFE(a) ^ HAS_EXEC_UNSAFE(b)) && \
 
79
         ((HAS_EXEC_PROFILE(a) && HAS_EXEC_PROFILE(b)) || \
 
80
          (HAS_EXEC_UNCONSTRAINED(a) && HAS_EXEC_UNCONSTRAINED(b))))
 
81
 
 
82
        /* walk the sorted table merging similar entries */
 
83
        for (cur = table[0], next = table[1], n = 1; next != NULL; n++, next = table[n]) {
 
84
                if (file_comp(&cur, &next) == 0) {
 
85
                        int conflict = CHECK_CONFLICT_UNSAFE(cur->mode, next->mode);
 
86
 
 
87
                        cur->mode |= next->mode;
 
88
                        /* check for merged x consistency */
 
89
                        if (HAS_MAY_EXEC(cur->mode) &&
 
90
                            (!AA_EXEC_SINGLE_MODIFIER_SET(cur->mode) ||
 
91
                             conflict)) {
 
92
                                PERROR(_("profile %s: has merged rule %s with multiple x modifiers\n"),
 
93
                                       cod->name, cur->name);
 
94
                                return 0;
 
95
                        }
 
96
                        free(next->name);
 
97
                        free(next);
 
98
                        table[n] = NULL;
 
99
                } else {
 
100
                        cur = next;
 
101
                }
 
102
        }
 
103
 
 
104
        /* rebuild the file_entry chain */
 
105
        cur = table[0];
 
106
        for (n = 1; n < count; n++) {
 
107
                if (table[n] != NULL) {
 
108
                        cur->next = table[n];
 
109
                        cur = table[n];
 
110
                }
 
111
        }
 
112
        cur->next = NULL;
 
113
        cod->entries = table[0];
 
114
 
 
115
        free(table);
 
116
 
 
117
        return 1;
 
118
}
 
119
 
 
120
static int process_net_entries(struct codomain __unused *cod)
 
121
{
 
122
        return 1;
 
123
}
 
124
 
 
125
int codomain_merge_rules(struct codomain *cod)
 
126
{
 
127
        if (!process_file_entries(cod))
 
128
                goto fail;
 
129
        if (!process_net_entries(cod))
 
130
                goto fail;
 
131
 
 
132
        /* XXX  return error from this */
 
133
        merge_hat_rules(cod);
 
134
 
 
135
        return 1;
 
136
fail:
 
137
        return 0;
 
138
}