blib/lib/Path/Secure.pm

filter: Statementstmt 23.6% 39 / 165 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 45.0% 9 / 20 Podpod 100% 4 / 4 totaltotal 15.5% 52 / 335 CC 60 SCAR 53.2
CC
60
Cov
14%
secure
462.0
__ANON__
210.0
__ANON__
182.0
CRAP 205.0
↑ Summary
1 package Path::Secure 0.001;
2  
3 # ABSTRACT: Say whether an account other than you could change a path, or make sure it cannot
4  
5 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Cwd qw{realpath};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Errno qw{ENOENT};
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use parent qw{Exporter};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Fcntl qw{S_ISDIR S_ISLNK S_ISVTX S_IWGRP S_IWOTH};
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our @EXPORT_OK = qw{exposed secure};
17  
18  
19 # The bits of a mode that chmod sets: the permissions, and setuid, setgid and sticky.
20 1 my $MODE_BITS = oct 7777;
21  
22 # Systems whose stat does not report POSIX owners and modes.
23 1 my %NOT_POSIX = map { $_ => 1 } qw{MSWin32 VMS dos os2 MacOS NetWare riscos symbian};
24  
25 # The longest chain of symbolic links that walk follows, as Linux does.
26 1 my $MOST_LINKS = 40;
27  
28 # The paths in @changed, as a message names them.
29 0 ▶my $listed = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
30 0 my (@changed) = @_;
31 0 ▶ return @changed ? join q{, }, @changed : 'nothing';
Branch: 1 branch
BranchTrueFalseTotal
@changed ? : 0 0 0
32 1 };
33  
34 # The options of $function, checked: $path, then @{$pairs}, of which @known
35 # are allowed. A hash of the options, with the gids of trust_group as the keys
36 # of trusted.
37 0 ▶my $options = sub {
Subroutine __ANON__: not called CC=14 SCAR=53.5
38 0 my ( $function, $path, $pairs, @known ) = @_;
39 0 ▶ die "Path::Secure cannot tell who can change a path on $^O, whose files do not have POSIX owners and modes\n" if $NOT_POSIX{$^O};
Branch: 1 branch
BranchTrueFalseTotal
if $NOT_POSIX{$^O} 0 0 0
40 0 ▶ die "$function needs a path\n" if !defined $path || $path eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $path or $path eq "" 0 0 0
Condition0%0/3
not defined $path or $path eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $path or $path eq ""
not defined $path $path eq ""
Truth table0%0/3
not(A) or B
A= not defined $pathB= $path eq ""
ABresult
1X1
011
000
41 0 ▶ die "$function takes a path and then pairs of options and their values\n" if @{$pairs} % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @{$pairs;} % 2 0 0 0
42 0 my %given = @{$pairs};
43 0 my %allowed = map { $_ => 1 } @known;
44 0 my @unknown = sort grep { !$allowed{$_} } keys %given;
45 0 ▶ die "$function does not know the option @unknown\n" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 0 0
46 0 ▶ my $groups = exists $given{trust_group} ? $given{trust_group} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $given{'trust_group'} ? : 0 0 0
47 0 ▶ my @gids = ref $groups eq 'ARRAY' ? @{$groups} : ($groups);
Branch: 1 branch
BranchTrueFalseTotal
ref $groups eq 'ARRAY' ? : 0 0 0
48  
49 0 for my $gid (@gids) {
50 0 ▶ my $shown = !defined $gid ? 'undef' : ref $gid ? ref $gid : $gid;
Branch: 2 branches
BranchTrueFalseTotal
!defined($gid) ? : 0 0 0
ref $gid ? : 0 0 0
51 0 ▶ die "trust_group takes a gid or a reference to an array of gids, not $shown\n" if !defined $gid || ref $gid || $gid !~ m{\A\d+\z}sx;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $gid or ref $gid or not $gid =~ /\A\d+\z/aasx 0 0 0
Condition0%0/6
not defined $gid or ref $gid or not $gid =~ /\A\d+\z/aasx
l!l&&r!l&&!r
000
not defined $gid or ref $gid
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $gid or ref $gid or not $gid =~ /\A\d+\z/aasx
not defined $gid ref $gid not $gid =~ /\A\d+\z/aasx
Truth table0%0/4
not(A) or B or not(C)
A= not defined $gidB= ref $gidC= not $gid =~ /\A\d+\z/aasx
ABCresult
1XX1
01X1
0011
0000
52 }
53 0 my %option = ( %given, trusted => { map { $_ => 1 } @gids } );
54 0 return \%option;
55 1 };
56  
57 # The names in $path that walk looks up, without the empty ones and dots.
58 0 ▶my $parts = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
59 0 my ($path) = @_;
60 0 ▶ return grep { $_ ne q{} && $_ ne q{.} } split m{/+}sx, $path;
Condition0%0/3
$_ ne '' && $_ ne '.'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne '' && $_ ne '.'
$_ ne '' $_ ne '.'
Truth table0%0/3
A && B
A= $_ ne ''B= $_ ne '.'
ABresult
0X0
100
111
61 1 };
62  
63 # Whether the owner $uid is one that the policy trusts.
64 0 ▶my $trusted_owner = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
65 0 my ($uid) = @_;
66 0 ▶ return $uid == $> || $uid == 0;
Condition0%0/3
$uid == $> || $uid == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$uid == $> || $uid == 0
$uid == $> $uid == 0
Truth table0%0/3
A || B
A= $uid == $>B= $uid == 0
ABresult
1X1
011
000
67 1 };
68  
69 # Why the entry $path, which lstat describes as @{$status}, is exposed, or
70 # undef. $role is 'above' for a directory above the target, else 'target'.
71 0 ▶my $judge = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
72 0 my ( $path, $status, $option, $mode, $role ) = @_;
73 0 my ( $bits, $uid, $gid ) = @{$status}[ 2, 4, 5 ];
74 0 my $trusted = $option->{trusted};
75 0 ▶ $bits = $bits & ~$MODE_BITS | $mode->{$path} if exists $mode->{$path};
Branch: 1 branch
BranchTrueFalseTotal
if exists $mode->{$path} 0 0 0
76 0 ▶ return "another account owns $path" if !$trusted_owner->($uid);
Branch: 1 branch
BranchTrueFalseTotal
unless &$trusted_owner($uid) 0 0 0
77 0 ▶ my $pass = $role eq 'above' || $option->{sticky_target};
Condition0%0/3
$role eq 'above' || $option->{'sticky_target'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$role eq 'above' || $option->{'sticky_target'}
$role eq 'above' $option->{'sticky_target'}
Truth table0%0/3
A || B
A= $role eq 'above'B= $option->{'sticky_target'}
ABresult
1X1
011
000
78 0 ▶ return if $pass && S_ISDIR($bits) && $bits & S_ISVTX;
Branch: 1 branch
BranchTrueFalseTotal
if $pass and S_ISDIR($bits) and $bits & 512 0 0 0
Condition0%0/6
$pass and S_ISDIR($bits) and $bits & 512
!ll&&!rl&&r
000
$pass and S_ISDIR($bits)
!ll&&!rl&&r
000
MC/DC0%0/3
$pass and S_ISDIR($bits) and $bits & 512
$pass S_ISDIR($bits) $bits & 512
Truth table0%0/4
A and B and C
A= $passB= S_ISDIR($bits)C= $bits & 512
ABCresult
0XX0
10X0
1100
1111
79 0 ▶ return "every account can write $path" if $bits & S_IWOTH;
Branch: 1 branch
BranchTrueFalseTotal
if $bits & 2 0 0 0
80 0 ▶ return "the group of $path can write it" if $bits & S_IWGRP && !$trusted->{$gid};
Branch: 1 branch
BranchTrueFalseTotal
if $bits & 16 and not $trusted->{$gid} 0 0 0
Condition0%0/3
$bits & 16 and not $trusted->{$gid}
!ll&&!rl&&r
000
MC/DC0%0/2
$bits & 16 and not $trusted->{$gid}
$bits & 16 not $trusted->{$gid}
Truth table0%0/3
A and not(B)
A= $bits & 16B= not $trusted->{$gid}
ABresult
0X0
100
111
81 0 return;
82 1 };
83  
84 # What exposed returns for $path, with the options in %{$option}, and with the
85 # modes of the real paths in %{$mode} in place of what lstat says.
86 0 ▶my $walk = sub {
Subroutine __ANON__: not called CC=13 SCAR=52.0
87 0 my ( $path, $option, $mode ) = @_;
88 0 my @todo = $parts->( File::Spec->rel2abs($path) );
89 0 my $at = q{/};
90 0 my @where = stat_entry($at);
91 0 my $links = 0;
92 0 ▶ while (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) 0 0 0
93 0 my $name = shift @todo;
94 0 ▶ if ( $name eq q{..} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "..") 0 0 0
95 0 $at =~ s{/[^/]+\z}{}sx;
96 0 ▶ $at = q{/} if $at eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $at eq "" 0 0 0
97 0 @where = stat_entry($at);
98 0 next;
99 }
100 0 my $why = $judge->( $at, \@where, $option, $mode, 'above' );
101 0 ▶ return $why if defined $why;
Branch: 1 branch
BranchTrueFalseTotal
if defined $why 0 0 0
102 0 ▶ my $next = $at eq q{/} ? "/$name" : "$at/$name";
Branch: 1 branch
BranchTrueFalseTotal
$at eq '/' ? : 0 0 0
103 0 my @found = stat_entry($next);
104 0 ▶ return $! == ENOENT ? "$next is not there" : "cannot look at $next: $!" if !@found;
Branch: 2 branches
BranchTrueFalseTotal
unless @found 0 0 0
$! == 2 ? : 0 0 0
105 0 ▶ if ( !S_ISLNK( $found[2] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (S_ISLNK($found[2])) 0 0 0
106 0 ( $at, @where ) = ( $next, @found );
107 0 next;
108 }
109 0 ▶ return "another account owns the symbolic link $next" if !$trusted_owner->( $found[4] );
Branch: 1 branch
BranchTrueFalseTotal
unless &$trusted_owner($found[4]) 0 0 0
110 0 ▶ return "$path names more than $MOST_LINKS symbolic links" if ++$links > $MOST_LINKS;
Branch: 1 branch
BranchTrueFalseTotal
if ++$links > $MOST_LINKS 0 0 0
111 0 my $to = readlink $next;
112 0 ▶ return "cannot read the symbolic link $next: $!" if !defined $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to 0 0 0
113 0 unshift @todo, $parts->($to);
114 0 ▶ if ( File::Spec->file_name_is_absolute($to) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->file_name_is_absolute($to)) 0 0 0
115 0 $at = q{/};
116 0 @where = stat_entry($at);
117 }
118 }
119 0 return $judge->( $at, \@where, $option, $mode, 'target' );
120 1 };
121  
122 # The entry at the real path of $path, as secure plans for it: its path, dev,
123 # ino, uid and gid, whether it is a directory, and the mode bits that chmod
124 # takes, as was.
125 0 ▶my $real_entry = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
126 0 my ($path) = @_;
127 0 my $real = realpath($path);
128 0 ▶ my @status = defined $real ? stat_entry($real) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $real ? : 0 0 0
129 0 ▶ die "$path is not there\n" if !@status;
Branch: 1 branch
BranchTrueFalseTotal
unless @status 0 0 0
130 0 return { path => $real, dev => $status[0], ino => $status[1], uid => $status[4], gid => $status[5], dir => S_ISDIR( $status[2] ), was => $status[2] & $MODE_BITS };
131 1 };
132  
133  
134 0 ▶sub exposed {
Subroutine exposed: not called CC=1 SCAR=6.9
Pod documented
135 0 my ( $path, @pairs ) = @_;
136 0 my $option = $options->( 'exposed', $path, \@pairs, qw{trust_group sticky_target} );
137 0 my $why = $walk->( $path, $option, {} );
138 0 return $why;
139 }
140  
141  
142 0 ▶sub secure {
Subroutine secure: not called CC=21 SCAR=61.4
Pod documented
143 0 my ( $path, @pairs ) = @_;
144 0 my $option = $options->( 'secure', $path, \@pairs, qw{upto trust_group private} );
145 0 ▶ die "secure needs upto, the highest directory that it may change\n" if !defined $option->{upto};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $option->{"upto"} 0 0 0
146 0 my $top = $real_entry->( $option->{upto} );
147 0 my $target = $real_entry->($path);
148 0 ▶ die "$option->{upto} is not a directory\n" if !$top->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $top->{"dir"} 0 0 0
149  
150 # Every entry from $top down to the target, each with the mode to give it.
151 0 my @plan = ($target);
152 0 my $at = $target->{path};
153 0 ▶ while ( $at ne $top->{path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at ne $top->{"path"}) 0 0 0
154 0 ▶ die "$path is not in $option->{upto}\n" if $at eq q{/};
Branch: 1 branch
BranchTrueFalseTotal
if $at eq "/" 0 0 0
155 0 $at =~ s{/[^/]+\z}{}sx;
156 0 ▶ $at = q{/} if $at eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $at eq "" 0 0 0
157 0 unshift @plan, $real_entry->($at);
158 }
159 0 my $trusted = $option->{trusted};
160 0 for my $entry (@plan) {
161 0 ▶ my $open = $entry->{was} & ( $trusted->{ $entry->{gid} } ? S_IWOTH : S_IWOTH | S_IWGRP );
Branch: 1 branch
BranchTrueFalseTotal
$$trusted{$entry->{'gid'}} ? : 0 0 0
162 0 $entry->{mode} = $entry->{was};
163 0 ▶ if ( $entry == $target || $option->{private} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($entry == $target or $option->{'private'}) { } 0 0 0
elsif ($open) { } 0 0 0
Condition0%0/3
$entry == $target or $option->{"private"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$entry == $target or $option->{"private"}
$entry == $target $option->{"private"}
Truth table0%0/3
A or B
A= $entry == $targetB= $option->{"private"}
ABresult
1X1
011
000
164 0 $entry->{mode} &= ~$open;
165 }
166 elsif ($open) {
167 0 $entry->{mode} |= S_ISVTX;
168 }
169 }
170  
171 0 my %mode = map { $_->{path} => $_->{mode} } @plan;
172 0 my $why = $walk->( $path, $option, \%mode );
173 0 ▶ die "secure cannot make $path safe: $why\n" if defined $why;
Branch: 1 branch
BranchTrueFalseTotal
if defined $why 0 0 0
174 0 my @changes = grep { $_->{mode} != $_->{was} } @plan;
175 0 for my $change (@changes) {
176 0 ▶ next if $change->{uid} == $> || $> == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $change->{"uid"} == $> or $> == 0 0 0 0
Condition0%0/3
$change->{"uid"} == $> or $> == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$change->{"uid"} == $> or $> == 0
$change->{"uid"} == $> $> == 0
Truth table0%0/3
A or B
A= $change->{"uid"} == $>B= $> == 0
ABresult
1X1
011
000
177 0 die "secure cannot make $path safe: $change->{path} needs its mode changed, and only root can do that\n";
178 }
179  
180 0 my @changed;
181 0 for my $change (@changes) {
182 0 my ( $dev, $ino, $now ) = stat_entry( $change->{path} );
183 0 ▶ if ( !defined $now || "$dev $ino" ne "@{$change}{qw{dev ino}}" || S_ISLNK($now) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $now or "$dev $ino" ne join($", @{$change;}{"dev", "ino"}) or S_ISLNK($now)) 0 0 0
Condition0%0/6
not defined $now or "$dev $ino" ne join($", @{$change;}{"dev", "ino"}) or S_ISLNK($now)
l!l&&r!l&&!r
000
not defined $now or "$dev $ino" ne join($", @{$change;}{"dev", "ino"})
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $now or "$dev $ino" ne join($", @{$change;}{"dev", "ino"}) or S_ISLNK($now)
not defined $now "$dev $ino" ne join($", @{$change;}{"dev", "ino"}) S_ISLNK($now)
Truth table0%0/4
not(A) or B or C
A= not defined $nowB= "$dev $ino" ne join($", @{$change;}{"dev", "ino"})C= S_ISLNK($now)
ABCresult
1XX1
01X1
0011
0000
184 0 die "secure stopped: $change->{path} changed while it worked; it had changed @{[ $listed->(@changed) ]}\n";
185 }
186 0 ▶ change_mode( @{$change}{qw{mode path}} ) or die "secure stopped: chmod $change->{path}: $!; it had changed @{[ $listed->(@changed) ]}\n";
Branch: 1 branch
BranchTrueFalseTotal
change_mode(@{$change;}{"mode", "path"}) or die qq[secure stopped: chmod $change->{"path"}: $!; it had changed @{[&$listed(@changed)];}\n] 0 0 0
187 0 push @changed, $change->{path};
188 }
189 0 $why = exposed( $path, trust_group => [ sort keys %{$trusted} ] );
190 0 ▶ die "secure changed @{[ $listed->(@changed) ]}, but $why\n" if defined $why;
Branch: 1 branch
BranchTrueFalseTotal
if defined $why 0 0 0
191 0 return @changed;
192 }
193  
194  
195 0 ▶sub stat_entry {
Subroutine stat_entry: not called CC=1 SCAR=6.9
Pod documented
196 0 my ($path) = @_;
197 0 return lstat $path;
198 }
199  
200  
201 0 ▶sub change_mode {
Subroutine change_mode: not called CC=1 SCAR=6.9
Pod documented
202 0 my ( $mode, $path ) = @_;
203 0 return chmod $mode, $path;
204 }
205  
206 1 1;
207  
208 __END__
↑ Summary