Secure/File.pm

filter: Statementstmt 72.2% 52 / 72 Branchbran 50.0% 28 / 56 Conditioncond 26.2% 16 / 61 MC/DCmcdc 3.8% 1 / 26 Subroutinesub 88.8% 8 / 9 Podpod 28.5% 2 / 7 totaltotal 46.3% 107 / 231 CC 50 SCAR 37.9
CC
50
Cov
51%
open_precheck
106.4
open
49.4
rw_check
20.0
CRAP 44.4
« CfgTie/TiePh.pm ↑ Summary CfgTie/CfgArgs.pm »
1 #!/usr/bin/perl -w
2 #Copyright 1998-2001, Randall Maas. All rights reserved. This program is free
3 #software; you can redistribute it and/or modify it under the same terms as
4 #PERL itself.
5  
6 =head1 NAME
7  
8 C<Secure::File> -- A module to open or create files within suid/sgid files
9  
10 =head1 SYNOPSIS
11  
12 use Secure::File;
13 my $SF = new Secure::File;
14 $SF->open();
15  
16 my $NF = new Secure::File, 'myfile';
17  
18  
19 =head1 DESCRIPTION
20  
21 C<open> This checks that both the effective and real user / group ids have
22 sufficient permissions to use the specified file. (Standard C<open> calls only
23 check the effective ids). C<Secure::File> also checks that the file we
24 open, really is the same file we checked ids on.
25  
26 If the file already exists, C<open> will fail.
27  
28 =head1 WARNING <==============================================================>
29  
30 B<DO NOT TRUST THIS MODULE>. Every effort has been made to make this module
31 useful, but it can not make a secure system out of an insecure one. It can not
32 read the programers mind.
33  
34 =head1 Author
35  
36 Randall Maas (L<mailto:randym@acm.org>, L<http://www.hamline.edu/~rcmaas/>)
37  
38 =cut
39  
40  
41 package Secure::File;
42 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 @ISA=qw(IO::File);
45 2 1;
46  
47 sub new
48 7 ▶{
Subroutine new: called CC=6 SCAR=18.6
Pod documented
49 7 my $self=shift;
50  
51 #Call the parent class new; we basically use IO::File's create
52 7 ▶ my $class=ref($self) || $self || "IO::File";
Condition40%2/5
ref $self || $self || 'IO::File'
l!l
70
ref $self or $self
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $self || $self || 'IO::File'
ref $self $self
Truth table33%1/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
53 7 my $R = $class->SUPER::new();
54  
55 #If it doesn't work, we give up and return to the caller.
56 7 ▶ return unless defined $R;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $R 0 7 7
57  
58 #If the caller passed some open() parameters, we will need to open the
59 #file as well
60 7 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 1 7
61 {
62 6 ▶ return unless $R->open(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless $R->open(@_) 2 4 6
63 }
64 5 return $R;
65 }
66  
67 sub open
68 7 ▶{
Subroutine open: called CC=20 SCAR=39.0
Pod documented
69 7 ▶ @_ >= 2 && @_ <= 4 or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])';
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 4 or croak("usage: \$fh->open(FILENAME [,MODE [,PERMS]])") 7 0 7
Condition33%1/3
@_ >= 2 and @_ <= 4
!ll&&!rl&&r
007
MC/DC0%0/2
@_ >= 2 and @_ <= 4
@_ >= 2 @_ <= 4
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 4
ABresult
0X0
100
111
70 7 my $self= shift;
71 7 ▶ my $class=ref($self) || $self || "IO::File";
Condition40%2/5
ref $self || $self || 'IO::File'
l!l
70
ref $self or $self
l!l&&r!l&&!r
700
MC/DC0%0/2
ref $self || $self || 'IO::File'
ref $self $self
Truth table33%1/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
72 7 my @S = open_precheck(@_);
73  
74 7 my $file = shift;
75 7 my $mode='';
76 7 ▶ if ($file=~s/^([<>]\s*)//) {$mode=$1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ s/^([<>]\s*)//) 7 0 7
77 7 ▶ if (! File::Spec->file_name_is_absolute($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($file)) 3 4 7
78 3 $file = File::Spec->catfile(File::Spec->curdir(),$file);
79 }
80  
81 7 my $x;
82  
83 7 ▶ if (@_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) { } 0 7 7
84 0 my $perms;
85 0 ($mode, $perms) = @_[2, 3];
86 0 ▶ if ($mode =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode =~ /^\d+$/) { } 0 0 0
87 0 ▶ defined $perms or $perms = 0666;
Branch: 1 branch
BranchTrueFalseTotal
defined $perms or $perms = 438 0 0 0
88 0 $x=sysopen($self, $file, $mode, $perms);
89 }
90 else
91 {
92 0 $file = IO::Handle::_open_mode_string($mode) . " $file\0";
93 0 $x = open($self, $file);
94 }
95 }
96 else
97 {
98 7 $x =open($self, $mode.$file);
99 }
100  
101 7 ▶ carp "Secure::File: Couldn't open $file" unless $x;
Branch: 1 branch
BranchTrueFalseTotal
unless $x 2 5 7
102 7 ▶ if ($mode =~ /^[>\sw]+$/ || ($mode =~ /^\d+$/ && $mode & O_WRONLY) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($mode =~ /^[>\sw]+$/ or $mode =~ /^\d+$/ and $mode & 1 or not @S and $mode =~ /w/ || $mode =~ /^\d+$/ && $mode & 2) 2 5 7
Condition50%9/18
$mode =~ /^[>\sw]+$/ or $mode =~ /^\d+$/ and $mode & 1 or not @S and $mode =~ /w/ || $mode =~ /^\d+$/ && $mode & 2
l!l&&r!l&&!r
205
$mode =~ /^[>\sw]+$/ or $mode =~ /^\d+$/ and $mode & 1
l!l&&r!l&&!r
205
$mode =~ /^\d+$/ and $mode & 1
!ll&&!rl&&r
500
not @S and $mode =~ /w/ || $mode =~ /^\d+$/ && $mode & 2
!ll&&!rl&&r
320
$mode =~ /w/ or $mode =~ /^\d+$/ and $mode & 2
l!l&&r!l&&!r
002
$mode =~ /^\d+$/ and $mode & 2
!ll&&!rl&&r
200
MC/DC14%1/7
$mode =~ /^[>\sw]+$/ or $mode =~ /^\d+$/ and $mode & 1 or not @S and $mode =~ /w/ || $mode =~ /^\d+$/ && $mode & 2
$mode =~ /^[>\sw]+$/ $mode =~ /^\d+$/ $mode & 1 not @S $mode =~ /w/ $mode =~ /^\d+$/ $mode & 2
Truth table25%3/12
A or B and C or not(D) and E or F and G
A= $mode =~ /^[>\sw]+$/B= $mode =~ /^\d+$/C= $mode & 1D= not @SE= $mode =~ /w/F= $mode =~ /^\d+$/G= $mode & 2
ABCDEFGresult
1XXXXXX1
011XXXX1
00X11XX1
00X10111
01011XX1
01010111
00X0XXX0
00X100X0
00X10100
0100XXX0
010100X0
01010100
103 (!@S && ($mode =~ /w/ || ($mode =~ /^\d+$/ && $mode & O_RDWR))))
104 {
105 2 return $x;
106 }
107 5 ▶ return 0 unless @S;
Branch: 1 branch
BranchTrueFalseTotal
unless @S 2 3 5
108 3 ▶ if ($self->handle_check(@S))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->handle_check(@S)) 3 0 3
109 {
110 3 return $self;
111 }
112 0 $self->close();
113 0 return 0;
114 }
115  
116 sub open_precheck($$)
117 7 ▶{
Subroutine open_precheck: called CC=13 SCAR=46.7
Pod undocumented
118 7 my $name=shift;
119 7 ▶ if ($name =~ /^<\s*([^\s]+)$/)
Branch: 4 branches
BranchTrueFalseTotal
if ($name =~ /^<\s*([^\s]+)$/) { } 5 2 7
elsif ($name =~ /^>\s*([^\s]+)$/) { } 2 0 2
elsif (@_ and defined $_[0] and $_[0] =~ /^\d+$/ && $_[0] & 1 || lc $_[0] eq 'w') { } 0 0 0
elsif (defined $_[0] and $_[0] =~ /r/i && $_[0] =~ /w/i || $_[0] =~ /^\d+$/ && $_[0] & 2) { } 0 0 0
120 {
121 5 r_check($1);
122 }
123 elsif ($name =~ /^>\s*([^\s]+)$/)
124 {
125 2 w_check($1);
126 }
127 ▶ elsif (@_ && defined $_[0] && (($_[0]=~/^\d+$/ && ($_[0] & O_WRONLY))
Condition0%0/12
@_ and defined $_[0] and $_[0] =~ /^\d+$/ && $_[0] & 1 || lc $_[0] eq "w"
!ll&&!rl&&r
000
@_ and defined $_[0]
!ll&&!rl&&r
000
$_[0] =~ /^\d+$/ and $_[0] & 1 or lc $_[0] eq "w"
l!l&&r!l&&!r
000
$_[0] =~ /^\d+$/ and $_[0] & 1
!ll&&!rl&&r
000
MC/DC0%0/5
@_ and defined $_[0] and $_[0] =~ /^\d+$/ && $_[0] & 1 || lc $_[0] eq "w"
@_ defined $_[0] $_[0] =~ /^\d+$/ $_[0] & 1 lc $_[0] eq "w"
Truth table0%0/7
A and B and C and D or E
A= @_B= defined $_[0]C= $_[0] =~ /^\d+$/D= $_[0] & 1E= lc $_[0] eq "w"
ABCDEresult
0XXXX0
10XXX0
110X00
111000
1111X1
110X11
111011
128 || lc($_[0]) eq 'w'))
129 {
130 0 w_check($name);
131 }
132 ▶ elsif (defined $_[0] && (($_[0] =~ /r/i && $_[0] =~ /w/i) ||
Condition0%0/12
defined $_[0] and $_[0] =~ /r/i && $_[0] =~ /w/i || $_[0] =~ /^\d+$/ && $_[0] & 2
!ll&&!rl&&r
000
$_[0] =~ /r/i and $_[0] =~ /w/i or $_[0] =~ /^\d+$/ and $_[0] & 2
l!l&&r!l&&!r
000
$_[0] =~ /r/i and $_[0] =~ /w/i
!ll&&!rl&&r
000
$_[0] =~ /^\d+$/ and $_[0] & 2
!ll&&!rl&&r
000
MC/DC0%0/5
defined $_[0] and $_[0] =~ /r/i && $_[0] =~ /w/i || $_[0] =~ /^\d+$/ && $_[0] & 2
defined $_[0] $_[0] =~ /r/i $_[0] =~ /w/i $_[0] =~ /^\d+$/ $_[0] & 2
Truth table0%0/8
A and B and C or D and E
A= defined $_[0]B= $_[0] =~ /r/iC= $_[0] =~ /w/iD= $_[0] =~ /^\d+$/E= $_[0] & 2
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
133 ($_[0] =~ /^\d+$/ && ($_[0] & O_RDWR))))
134 {
135 0 rw_check($name);
136 }
137 else
138 {
139 0 r_check($name);
140 }
141 }
142  
143 sub r_check
144 5 ▶{
Subroutine r_check: called CC=3 SCAR=11.0
Pod undocumented
145 #Check to see if the real user has read privileges
146 5 my @S=stat($_[0]);
147 5 ▶ if (!@S) {return;}
Branch: 1 branch
BranchTrueFalseTotal
unless (@S) 2 3 5
148 3 ▶ return @S if -R _;
Branch: 1 branch
BranchTrueFalseTotal
if -R _ 3 0 3
149 }
150  
151 sub w_check
152 2 ▶{
Subroutine w_check: called CC=5 SCAR=20.2
Pod undocumented
153 #Check to see if the real user has write privileges
154 2 my @S=stat($_[0]);
155 2 ▶ if (!@S)
Branch: 1 branch
BranchTrueFalseTotal
unless (@S) 2 0 2
156 {
157 2 ▶ if ($_[0] =~ /^(.*)\/[^\/]+$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ m[^(.*)/[^/]+$]) 2 0 2
158 {
159 2 @S=stat($1);
160 2 ▶ return @S if -W _;
Branch: 1 branch
BranchTrueFalseTotal
if -W _ 2 0 2
161 }
162 0 return;
163 }
164 0 ▶ return @S if -W _;
Branch: 1 branch
BranchTrueFalseTotal
if -W _ 0 0 0
165 }
166  
167 sub rw_check
168 0 ▶{
Subroutine rw_check: not called CC=4 SCAR=30.0
Pod undocumented
169 #Check to see if the real user has read/write privileges
170 0 my @S=stat($_[0]);
171 0 ▶ if (!@S) {return;}
Branch: 1 branch
BranchTrueFalseTotal
unless (@S) 0 0 0
172 0 ▶ return undef if !-R _;
Branch: 1 branch
BranchTrueFalseTotal
unless -R _ 0 0 0
173 0 ▶ return @S if -W _;
Branch: 1 branch
BranchTrueFalseTotal
if -W _ 0 0 0
174 }
175  
176 sub handle_check
177 3 ▶{
Subroutine handle_check: called CC=5 SCAR=19.6
Pod undocumented
178 #Check to be sure that the inode has not changed!
179 3 my $Handle = shift;
180  
181 #Get the information on the file
182 3 my @S2 = $Handle->stat;
183  
184 #If the file doesn't exist, return false;
185 3 ▶ return 0 unless @S2;
Branch: 1 branch
BranchTrueFalseTotal
unless @S2 0 3 3
186  
187 #Return true if and only if the file has the same ID:
188 # That is: its dev, rdev, inode all match
189 0 ▶ if ($_[0] != $S2[0] || $_[1] != $S2[1] || $_[6] != $S2[6]) {return 0;}
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] != $S2[0] or $_[1] != $S2[1] or $_[6] != $S2[6]) 0 3 3
Condition33%2/6
$_[0] != $S2[0] or $_[1] != $S2[1] or $_[6] != $S2[6]
l!l&&r!l&&!r
003
$_[0] != $S2[0] or $_[1] != $S2[1]
l!l&&r!l&&!r
003
MC/DC0%0/3
$_[0] != $S2[0] or $_[1] != $S2[1] or $_[6] != $S2[6]
$_[0] != $S2[0] $_[1] != $S2[1] $_[6] != $S2[6]
Truth table25%1/4
A or B or C
A= $_[0] != $S2[0]B= $_[1] != $S2[1]C= $_[6] != $S2[6]
ABCresult
1XX1
01X1
0011
0000
190 3 1;
191 }
« CfgTie/TiePh.pm ↑ Summary CfgTie/CfgArgs.pm »