~ubuntu-branches/ubuntu/precise/policycoreutils/precise

« back to all changes in this revision

Viewing changes to audit2allow/audit2allow.perl

  • Committer: Bazaar Package Importer
  • Author(s): Manoj Srivastava
  • Date: 2007-04-20 10:53:23 UTC
  • mfrom: (1.2.7 upstream) (3.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20070420105323-zqj86zw2nr99t2bt
Tags: 2.0.7-1
* New upstream trunk release
  * Merged sepolgen and audit2allow patches to leave generated files in
    the current directory from Karl MacMillan. 
  * Merged small fix to correct include of errcodes.h in semodule_deps
    from Dan Walsh. 
  * Merged new audit2allow from Karl MacMillan. This audit2allow depends
    on the new sepolgen python module. Note that you must run the
    sepolgen-ifgen tool to generate the data needed by audit2allow to
    generate refpolicy. 

* Added build and runtime dependencies on sepolgen
* Fixed watch file to correctly reflect the fact that this is the trunk
  version. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl
2
 
 
3
 
#    Adapted from:
4
 
#    newrules.pl, Copyright (C) 2001 Justin R. Smith (jsmith@mcs.drexel.edu)
5
 
#
6
 
#    This program is free software; you can redistribute it and/or
7
 
#    modify it under the terms of the GNU General Public License as
8
 
#    published by the Free Software Foundation; either version 2 of
9
 
#    the License, or (at your option) any later version.
10
 
#
11
 
#    This program is distributed in the hope that it will be useful,
12
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
#    GNU General Public License for more details.
15
 
#
16
 
#    You should have received a copy of the GNU General Public License
17
 
#    along with this program; if not, write to the Free Software
18
 
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA     
19
 
#                                        02111-1307  USA
20
 
#    2003 Oct 11: Add -l option by Yuichi Nakamura(ynakam@users.sourceforge.jp)
21
 
 
22
 
 
23
 
$load_policy_pattern="avc:.*granted.*{.*load_policy.*}";
24
 
 
25
 
while ($opt = shift @ARGV) {
26
 
        if ($opt eq "-d") { $read_dmesg++; }
27
 
        elsif ($opt eq "-v") { $verbose++; }
28
 
        elsif ($opt eq "-i") { $input = shift @ARGV; }
29
 
        elsif ($opt eq "-o") { $output= shift @ARGV; }
30
 
        elsif ($opt eq "-l") { $load_policy++; }
31
 
        elsif ($opt eq "--help") { &printUsage; }
32
 
                else  { print "unknown option, '$opt'\n\n"; &printUsage; }
33
 
}
34
 
 
35
 
if ($read_dmesg && $input) {
36
 
        print "Error, can't read from both dmesg and $input\n\n";
37
 
        &printUsage;
38
 
}
39
 
 
40
 
if ($read_dmesg) { open (IN, "/bin/dmesg|"); } 
41
 
elsif ($input)   { open (IN, "$input");      }
42
 
else             { open (IN, "-");           }  # STDIN
43
 
 
44
 
if ($output)     { open (OUT, ">>$output");   }
45
 
else             { open (OUT, ">-");         }  # STDOUT
46
 
 
47
 
if($load_policy){ #store logs after last "load_policy" in @log_buf
48
 
    while ($line = <IN>) {
49
 
        if($line=~/$load_policy_pattern/) {
50
 
             #stored logs are unnecessary
51
 
             undef @log_buf;
52
 
        }
53
 
        else
54
 
        {
55
 
            push @log_buf,$line;
56
 
        }
57
 
    }
58
 
}
59
 
 
60
 
while ($line=&readNewline) {
61
 
    next unless ($line =~ m/avc:\s*denied\s*\{((\w|\s)*)\}/);
62
 
    @types=split /\ /,$line;
63
 
    $info="";
64
 
    $group="";
65
 
    $command="";
66
 
    foreach $i(0..$#types){
67
 
        next if($types[$i]!~/[=\{]/);
68
 
       if($types[$i]=~/^\{/){
69
 
            $j=$i+1;
70
 
            while($types[$j]!~/\}/){
71
 
                $command.=" $types[$j]";
72
 
                $j++;
73
 
            }
74
 
            next;
75
 
        }
76
 
        my($a,$b) = split /=/,$types[$i];
77
 
        
78
 
        next if($a eq "pid");
79
 
        next if($a eq "dev");
80
 
        next if($a eq "ino");
81
 
        
82
 
        if(($a eq "scontext")||($a eq "tcontext")||($a eq "tclass")){
83
 
            if($a ne "tclass"){
84
 
                my($c,$c,$c) = split /:/, $b;
85
 
                $b=$c;
86
 
            }
87
 
            $b=~s/\n//;
88
 
            $group.="|$b";
89
 
            next;
90
 
        }
91
 
        $b=~s/:\[\d+\]//;
92
 
        $a=uc $a;
93
 
        $info.="$a=$b  "; 
94
 
    }
95
 
    
96
 
    my($c,$c,$c,$c) = split /\|/, $group;
97
 
    $info=~s/\ $c=\S+\ //gi;
98
 
    # escape regexp patterns --<g>
99
 
    $info=~s/([^\w])/\\$1/g;
100
 
   
101
 
    @atypes=split /\ /,$command;
102
 
    foreach $i(0..$#atypes){
103
 
        $rules{$group}{$atypes[$i]}++;
104
 
    }
105
 
    
106
 
    $info.=" ";
107
 
    if($occur{$group}!~$info){
108
 
        $occur{$group}.="\t#$info: $command\n";
109
 
    }
110
 
    else{       
111
 
        my ($a,$b) = split /$info:\ /, $occur{$group};
112
 
        my ($temp) = split /\n/, $b;
113
 
        
114
 
        @com=split /\ /, $command;
115
 
        foreach $i(1..$#com){
116
 
            $b=" $com[$i]$b" if($temp!~$com[$i]);
117
 
        }
118
 
        $occur{$group}="$a$info: $b";
119
 
    }
120
 
}
121
 
 
122
 
# done  with the input file
123
 
# now generate the rules
124
 
foreach $k (sort keys %rules)
125
 
126
 
    my ($a,$scontext,$tcontext,$tclass) = split /\|/, $k;
127
 
    if ($scontext eq $tcontext) {
128
 
        $tcontext = 'self';
129
 
    }
130
 
    print OUT  "allow $scontext $tcontext:$tclass";
131
 
    
132
 
    my $access_types = $rules{$k};
133
 
    $len=(keys %$access_types);
134
 
    if ($len gt 2 ) { print OUT  " {"; }
135
 
    foreach $t (sort keys %$access_types) {
136
 
      if ($t ne "") {print OUT  " $t";}
137
 
    }
138
 
    if ($len gt 2 ) { print OUT " }"; }
139
 
    print OUT ";\n";
140
 
    $occur{$k} =~ s/\\(.)/$1/g;  # de-escape string
141
 
    print OUT "$occur{$k}\n" if ($verbose);
142
 
}
143
 
 
144
 
exit;
145
 
 
146
 
sub readNewline {
147
 
    if($load_policy){
148
 
        $newline=shift @log_buf;
149
 
    }else{
150
 
        $newline=<IN>;
151
 
    }
152
 
    return $newline;
153
 
}
154
 
 
155
 
sub printUsage {
156
 
        print "audit2allow [-d] [-v] [-l] [-i <inputfile> ] [-o <outputfile>]
157
 
        -d      read input from output of /bin/dmesg
158
 
        -v      verbose output
159
 
        -l      read input only after last \"load_policy\"
160
 
        -i      read input from <inputfile>
161
 
        -o      append output to <outputfile>\n";
162
 
        exit;
163
 
}
164