-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaudit2allow.perl
More file actions
164 lines (143 loc) · 4.28 KB
/
Copy pathaudit2allow.perl
File metadata and controls
164 lines (143 loc) · 4.28 KB
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
#!/usr/bin/perl
# Adapted from:
# newrules.pl, Copyright (C) 2001 Justin R. Smith (jsmith@mcs.drexel.edu)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA
# 2003 Oct 11: Add -l option by Yuichi Nakamura(ynakam@users.sourceforge.jp)
$load_policy_pattern="avc:.*granted.*{.*load_policy.*}";
while ($opt = shift @ARGV) {
if ($opt eq "-d") { $read_dmesg++; }
elsif ($opt eq "-v") { $verbose++; }
elsif ($opt eq "-i") { $input = shift @ARGV; }
elsif ($opt eq "-o") { $output= shift @ARGV; }
elsif ($opt eq "-l") { $load_policy++; }
elsif ($opt eq "--help") { &printUsage; }
else { print "unknown option, '$opt'\n\n"; &printUsage; }
}
if ($read_dmesg && $input) {
print "Error, can't read from both dmesg and $input\n\n";
&printUsage;
}
if ($read_dmesg) { open (IN, "/bin/dmesg|"); }
elsif ($input) { open (IN, "$input"); }
else { open (IN, "-"); } # STDIN
if ($output) { open (OUT, ">>$output"); }
else { open (OUT, ">-"); } # STDOUT
if($load_policy){ #store logs after last "load_policy" in @log_buf
while ($line = <IN>) {
if($line=~/$load_policy_pattern/) {
#stored logs are unnecessary
undef @log_buf;
}
else
{
push @log_buf,$line;
}
}
}
while ($line=&readNewline) {
next unless ($line =~ m/avc:\s*denied\s*\{((\w|\s)*)\}/);
@types=split /\ /,$line;
$info="";
$group="";
$command="";
foreach $i(0..$#types){
next if($types[$i]!~/[=\{]/);
if($types[$i]=~/^\{/){
$j=$i+1;
while($types[$j]!~/\}/){
$command.=" $types[$j]";
$j++;
}
next;
}
my($a,$b) = split /=/,$types[$i];
next if($a eq "pid");
next if($a eq "dev");
next if($a eq "ino");
if(($a eq "scontext")||($a eq "tcontext")||($a eq "tclass")){
if($a ne "tclass"){
my($c,$c,$c) = split /:/, $b;
$b=$c;
}
$b=~s/\n//;
$group.="|$b";
next;
}
$b=~s/:\[\d+\]//;
$a=uc $a;
$info.="$a=$b ";
}
my($c,$c,$c,$c) = split /\|/, $group;
$info=~s/\ $c=\S+\ //gi;
# escape regexp patterns --<g>
$info=~s/([^\w])/\\$1/g;
@atypes=split /\ /,$command;
foreach $i(0..$#atypes){
$rules{$group}{$atypes[$i]}++;
}
$info.=" ";
if($occur{$group}!~$info){
$occur{$group}.="\t#$info: $command\n";
}
else{
my ($a,$b) = split /$info:\ /, $occur{$group};
my ($temp) = split /\n/, $b;
@com=split /\ /, $command;
foreach $i(1..$#com){
$b=" $com[$i]$b" if($temp!~$com[$i]);
}
$occur{$group}="$a$info: $b";
}
}
# done with the input file
# now generate the rules
foreach $k (sort keys %rules)
{
my ($a,$scontext,$tcontext,$tclass) = split /\|/, $k;
if ($scontext eq $tcontext) {
$tcontext = 'self';
}
print OUT "allow $scontext $tcontext:$tclass";
my $access_types = $rules{$k};
$len=(keys %$access_types);
if ($len gt 2 ) { print OUT " {"; }
foreach $t (sort keys %$access_types) {
if ($t ne "") {print OUT " $t";}
}
if ($len gt 2 ) { print OUT " }"; }
print OUT ";\n";
$occur{$k} =~ s/\\(.)/$1/g; # de-escape string
print OUT "$occur{$k}\n" if ($verbose);
}
exit;
sub readNewline {
if($load_policy){
$newline=shift @log_buf;
}else{
$newline=<IN>;
}
return $newline;
}
sub printUsage {
print "audit2allow [-d] [-v] [-l] [-i <inputfile> ] [-o <outputfile>]
-d read input from output of /bin/dmesg
-v verbose output
-l read input only after last \"load_policy\"
-i read input from <inputfile>
-o append output to <outputfile>\n";
exit;
}