| 1 | package Filesys::Restrict; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 6 | our $VERSION; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 6 | require XSLoader; | |||||||||||||||||||||||||||
| 12 | 6 | XSLoader::load(__PACKAGE__, $VERSION); | |||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Filesys::Restrict - Restrict filesystem access | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | { | ||||||||||||||||||||||||||||
| 24 | my $check = Filesys::Restrict::create( | ||||||||||||||||||||||||||||
| 25 | sub { | ||||||||||||||||||||||||||||
| 26 | my ($op, $path) = @_; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | return 1 if $path =~ m<^/safe/place/>; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Deny access to anything else: | ||||||||||||||||||||||||||||
| 31 | return 0; | ||||||||||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # In this block, most Perl code will throw if it tries | ||||||||||||||||||||||||||||
| 36 | # to access anything outside of /safe/place. | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # No more filesystem checks here. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | This module is a reasonable-best-effort at preventing Perl code from | ||||||||||||||||||||||||||||
| 44 | accessing files you don’t want to allow. One potential application of | ||||||||||||||||||||||||||||
| 45 | this is to restrict filesystem access to F</tmp> in tests. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 B<THIS> B<IS> B<NOT> B<A> B<SECURITY> B<TOOL!> | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | This module cannot prevent all unintended filesystem access. | ||||||||||||||||||||||||||||
| 50 | The following are some known ways to circumvent it: | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =over | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =item * Use XS modules (e.g., L<POSIX>). | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =item * Use one of C<open()>’s more esoteric forms. | ||||||||||||||||||||||||||||
| 57 | This module tries to parse typical C<open()> arguments but doesn’t | ||||||||||||||||||||||||||||
| 58 | “bend over backward”. The 2- and 3-argument forms are assumed to be | ||||||||||||||||||||||||||||
| 59 | valid if there’s an unrecognized format, and we ignore the 1-argument | ||||||||||||||||||||||||||||
| 60 | form entirely. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =item * Call C<system()>, C<do()>, or C<require()>. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | We I<could> actually restrict C<do()> and C<require()>. | ||||||||||||||||||||||||||||
| 65 | These, though, are a bit different from other built-ins because they | ||||||||||||||||||||||||||||
| 66 | don’t facilitate reading arbitrary data off the filesystem; rather, | ||||||||||||||||||||||||||||
| 67 | they’re narrowly-scoped to bringing in additional Perl code. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | If you have a use case where it’s useful to restrict these, | ||||||||||||||||||||||||||||
| 70 | file a feature request. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =back | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | L<Test::MockFile> can achieve a similar effect to this module but | ||||||||||||||||||||||||||||
| 77 | has some compatibility problems with some Perl syntax. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Linux’s L<fanotify(7)> provides a method of real-time access control | ||||||||||||||||||||||||||||
| 80 | via the kernel. See L<Linux::Fanotify> and L<Linux::Perl> for Perl | ||||||||||||||||||||||||||||
| 81 | implementations. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 6 | ▶ | use Filesys::Restrict::X (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 6 | our $_AUTHORIZE = undef; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head2 $obj = create( sub { .. } ) | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Creates an opaque object that installs an access-control callback. | ||||||||||||||||||||||||||||
| 98 | Any existing access-control callback is saved and restored whenever | ||||||||||||||||||||||||||||
| 99 | $obj is DESTROYed. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | The access-control callback is called with two arguments: | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =over | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =item * The name of the Perl op that requests filesystem access. | ||||||||||||||||||||||||||||
| 106 | The names come from C<PL_op_desc> in Perl’s L<opcode.h> header file; | ||||||||||||||||||||||||||||
| 107 | they should correlate to the actual built-in called. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =item * The filesystem path in question. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =back | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | The callback can end in one of three ways: | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =over | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =item * Return truthy to confirm access to the path. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =item * Return falsy to cause a L<Filesys::Restrict::X::Forbidden> | ||||||||||||||||||||||||||||
| 120 | instance to be thrown. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =item * Throw a custom exception. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =back | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 7 | ▶ | sub create { | ||||||||||||||||||||||||||
|
Subroutine
create: called CC=5 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 7 | ▶ | die 'Void context is meaningless!' if !defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 7 | my $cb = $_[0]; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 7 | ▶ | if (!$cb) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | die( (caller 0)[3] . ' requires a callback!' ); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 7 | ▶ | if (!ref($cb) || !UNIVERSAL::isa($cb, 'CODE')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $cb and UNIVERSAL::isa($cb, "CODE")
MC/DC0%0/2
ref $cb and UNIVERSAL::isa($cb, "CODE")
ref $cb
UNIVERSAL::isa($cb, "CODE")
Truth table33%1/3
A and B
A= ref $cbB= UNIVERSAL::isa($cb, "CODE")
| |||||||||||||||||||||||||||||
| 138 | 0 | die( (caller 0)[3] . " requires a callback, not “$cb”!" ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 7 | my $stored_cb = $_AUTHORIZE; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 7 | $_AUTHORIZE = $cb; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 7 | return bless \$stored_cb, 'Filesys::Restrict::Guard'; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 57 | ▶ | sub _CROAK { | ||||||||||||||||||||||||||
|
Subroutine
_CROAK: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 149 | 57 | local $_AUTHORIZE; | |||||||||||||||||||||||||||
| 150 | 57 | die Filesys::Restrict::X->create('Forbidden', @_); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | package Filesys::Restrict::Guard; | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 7 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 158 | 7 | $Filesys::Restrict::_AUTHORIZE = ${ $_[0] }; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 6 | 1; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 LICENSE & COPYRIGHT | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Copyright 2022 Gasper Software Consulting. All rights reserved. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | This library is licensed under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 170 | See L<perlartistic>. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | This library was originally a research project at | ||||||||||||||||||||||||||||
| 173 | L<cPanel, L.L.C.|https://cpanel.net>. | ||||||||||||||||||||||||||||