File: gen-xtrans.pl

package info (click to toggle)
apparmor 2.7.103-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 11,920 kB
  • sloc: ansic: 12,022; perl: 10,644; sh: 8,119; cpp: 2,505; yacc: 1,592; python: 1,489; makefile: 1,138; lex: 1,003; pascal: 399; ruby: 374; exp: 250; java: 212; xml: 159
file content (235 lines) | stat: -rwxr-xr-x 6,180 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/perl

use strict;
use Locale::gettext;
use POSIX;

setlocale(LC_MESSAGES, "");

my $prefix="simple_tests/generated_x";
my $prefix_leading="simple_tests/generated_perms_leading";
my $prefix_safe="simple_tests/generated_perms_safe";

my @trans_types = ("p", "P", "c", "C", "u", "i");
my @modifiers = ("i", "u");
my %trans_modifiers = (
    "p" => \@modifiers,
    "P" => \@modifiers,
    "c" => \@modifiers,
    "C" => \@modifiers,
    );

my @targets = ("", "target", "target2");
my @null_target = ("");

my %named_trans = (
    "p" => \@targets,
    "P" => \@targets,
    "c" => \@targets,
    "C" => \@targets,
    "u" => \@null_target,
    "i" => \@null_target,
    );

my %safe_map = (
    "p" => "unsafe",
    "P" => "safe",
    "c" => "unsafe",
    "C" => "safe",
    "u" => "",
    "i" => "",
    );

my %invert_safe = (
    "safe" => "unsafe",
    "unsafe" => "safe",
    );

# audit qualifier disabled for now it really shouldn't affect the conflict
# test but it may be worth checking every once in awhile
#my @qualifiers = ("", "owner", "audit", "audit owner");
my @qualifiers = ("", "owner");

my $count = 0;

gen_conflicting_x();
gen_overlap_re_exact();
gen_dominate_re_re();
gen_ambiguous_re_re();
gen_leading_perms("exact", "/bin/cat", "/bin/cat");
gen_leading_perms("exact-re", "/bin/*", "/bin/*");
gen_leading_perms("overlap", "/*", "/bin/cat");
gen_leading_perms("dominate", "/**", "/*");
gen_leading_perms("ambiguous", "/a*", "/*b");
gen_safe_perms("exact", "PASS", "", "/bin/cat", "/bin/cat");
gen_safe_perms("exact-re", "PASS", "", "/bin/*", "/bin/*");
gen_safe_perms("overlap", "PASS", "", "/*", "/bin/cat");
gen_safe_perms("dominate", "PASS", "", "/**", "/*");
gen_safe_perms("ambiguous", "PASS", "", "/a*", "/*b");
gen_safe_perms("exact", "FAIL", "inv", "/bin/cat", "/bin/cat");
gen_safe_perms("exact-re", "FAIL", "inv", "/bin/*", "/bin/*");
gen_safe_perms("overlap", "PASS", "inv", "/*", "/bin/cat");
gen_safe_perms("dominate", "FAIL", "inv", "/**", "/*");
gen_safe_perms("ambiguous", "FAIL", "inv", "/a*", "/*b");

print "Generated $count xtransition interaction tests\n";

sub gen_list {
    my @output;
    foreach my $trans (@trans_types) {
	if ($trans_modifiers{$trans}) {
	    foreach my $mod (@{$trans_modifiers{$trans}}) {
		push @output, "${trans}${mod}x";
	    }
	}
	push @output, "${trans}x";
    }
    return @output;
}

sub print_rule($$$$$$) {
    my ($file, $leading, $qual, $name, $perm, $target) = @_;
    if ($leading) {
	print $file "\t${qual} ${perm} ${name}";
    } else {
	print $file "\t${qual} ${name} ${perm}";
    }
    if ($target ne "") {
	print $file " -> $target";
    }
    print $file ",\n";
}

sub gen_file($$$$$$$$$$$$) {
    my ($name, $xres, $leading1, $qual1, $rule1, $perm1, $target1, $leading2, $qual2, $rule2, $perm2, $target2) = @_;

#    print "$xres $rule1 $perm1 $target1 $rule2 $perm2 $target2\n";

    my $file;
    unless (open $file, ">$name") {
	print("couldn't open $name\n");
	exit 1;
    }

    print $file "#\n";
    print $file "#=DESCRIPTION ${name}\n";
    print $file "#=EXRESULT ${xres}\n";
    print $file "#\n";
    print $file "/usr/bin/foo {\n";
    print_rule($file, $leading1, $qual1, $rule1, $perm1, $target1);
    print_rule($file, $leading2, $qual2, $rule2, $perm2, $target2);
    print $file "}\n";
    close($file);

    $count++;
}

#NOTE: currently we don't do px to cx, or cx to px conversion
#      so
# /foo {
#    /* px -> /foo//bar,
#    /* cx -> bar,
#
# will conflict
#
#NOTE: conflict tests don't tests leading permissions or using unsafe keywords
#      It is assumed that there are extra tests to verify 1 to 1 coorispondance
sub gen_files($$$$) {
    my ($name, $rule1, $rule2, $default) = @_;

    my @perms = gen_list();

#    print "@perms\n";

    foreach my $i (@perms) {
	foreach my $t (@{$named_trans{substr($i, 0, 1)}}) {
	    foreach my $q (@qualifiers) {
		foreach my $j (@perms) {
		    foreach my $u (@{$named_trans{substr($j, 0, 1)}}) {
			foreach my $r (@qualifiers) {
			    my $file="${prefix}/${name}-$q$i$t-$r$j$u.sd";
#		    print "$file\n";

		    #override failures when transitions are the same
			    my $xres = ${default};
			    if ($i eq $j && $t eq $u) {
				$xres = "PASS";
			    }


#		    print "foo $xres $rule1 $i $t $rule2 $j $u\n";
			    gen_file($file, $xres, 0, $q, $rule1, $i, $t, 0, $r, $rule2, $j, $u);
			}
		    }
		}
	    }
	}
    }

}

sub gen_conflicting_x {
    gen_files("conflict", "/bin/cat", "/bin/cat", "FAIL");
}

sub gen_overlap_re_exact {

    gen_files("exact", "/bin/cat", "/bin/*", "PASS");
}

# we currently don't support this, once supported change to "PASS"
sub gen_dominate_re_re {
    gen_files("dominate", "/bin/*", "/bin/**", "FAIL");
}

sub gen_ambiguous_re_re {
    gen_files("ambiguous", "/bin/a*", "/bin/*b", "FAIL");
}


# test that rules that lead with permissions don't conflict with
# the same rule using trailing permissions.
sub gen_leading_perms($$$) {
    my ($name, $rule1, $rule2) = @_;

    my @perms = gen_list();

    foreach my $i (@perms) {
	foreach my $t (@{$named_trans{substr($i, 0, 1)}}) {
	    foreach my $q (@qualifiers) {
		my $file="${prefix_leading}/${name}-$q$i$t.sd";
#		    print "$file\n";

		gen_file($file, "PASS", 0, $q, $rule1, $i, $t, 1, $q, $rule2, $i, $t);
	    }
	}
    }
}

# test for rules with leading safe or unsafe keywords.
# check they are equivalent to their counter part,
# or if $invert that they properly conflict with their counterpart
sub gen_safe_perms($$$$$) {
    my ($name, $xres, $invert, $rule1, $rule2) = @_;

    my @perms = gen_list();

    foreach my $i (@perms) {
	foreach my $t (@{$named_trans{substr($i, 0, 1)}}) {
	    foreach my $q (@qualifiers) {
		my $qual = $safe_map{substr($i, 0, 1)};
		if ($invert) {
		    $qual = $invert_safe{$qual};
		}
		if (! $invert || $qual) {
		    my $file="${prefix_safe}/${name}-$invert-$q${qual}-rule-$i$t.sd";
#		    print "$file\n";
		    gen_file($file, $xres, 0, "$q $qual", $rule1, $i, $t, 1, $q, $rule2, $i, $t);
		    $file="${prefix_safe}/${name}-$invert-$q$qual${i}-rule-$t.sd";
		    gen_file($file, $xres, 0, $q, $rule1, $i, $t, 1, "$q $qual", $rule2, $i, $t);
		}

	    }
	}
    }
}