| 1 | package Filesys::POSIX::ReducedPrivileges; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Copyright (c) 2016, cPanel, Inc. | ||||||||||||||||||||||||||||
| 4 | # All rights reserved. | ||||||||||||||||||||||||||||
| 5 | # http://cpanel.net/ | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # This is free software; you can redistribute it and/or modify it under the same | ||||||||||||||||||||||||||||
| 8 | # terms as Perl itself. See the LICENSE file for further details. | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Filesys::POSIX::Error qw(throw); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Filesys::POSIX::ReducedPrivileges::Inode (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Filesys::POSIX::Real; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Errno (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | our @ISA = qw(Filesys::POSIX::Real); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Filesys::POSIX::ReducedPrivileges - Portal to actual underlying filesystem as seen by a particular UID/GID. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | use Filesys::POSIX; | ||||||||||||||||||||||||||||
| 25 | use Filesys::POSIX::Real; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | my $fs = Filesys::POSIX->new(Filesys::POSIX::ReducedPrivileges->new, | ||||||||||||||||||||||||||||
| 28 | 'path' => '/home/foo/test', | ||||||||||||||||||||||||||||
| 29 | 'noatime' => 1, | ||||||||||||||||||||||||||||
| 30 | 'uid' => 99, | ||||||||||||||||||||||||||||
| 31 | 'gid' => 99, | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | This module wraps the L<Filesys::POSIX::Real> filesystem type with entry and | ||||||||||||||||||||||||||||
| 37 | exit functions that switch the effective UID and GID whenever the filesystem | ||||||||||||||||||||||||||||
| 38 | is accessed. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 MOUNT OPTIONS | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | The following values are mandatory: | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =over | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =item C<path> | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | The path, in the real filesystem, upon which the new filesystem to be mounted | ||||||||||||||||||||||||||||
| 49 | will be based. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =item C<uid> | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | The numeric UID to use when accessing the real filesystem. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =item C<gid> | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | The numeric GID to use when accessing the real filesystem. The suppelemental | ||||||||||||||||||||||||||||
| 58 | group list is also limited to this GID. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =back | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 0 | my ( $class, %opts ) = @_; | |||||||||||||||||||||||||||
| 66 | 0 | my $self = $class->SUPER::new(); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | return $self; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 0 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | my $path = $opts{'path'} or throw &Errno::EINVAL; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | $self->{_uid} = $opts{uid}; | |||||||||||||||||||||||||||
| 78 | 0 | $self->{_gid} = "$opts{gid} $opts{gid}"; | |||||||||||||||||||||||||||
| 79 | 0 | $self->{_privileges_reduced} = 0; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my $root = Filesys::POSIX::ReducedPrivileges::Inode->from_disk( $path, 'dev' => $self ); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | throw &Errno::ENOTDIR unless $root->dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | $self->{'flags'} = \%opts; | |||||||||||||||||||||||||||
| 86 | 0 | $self->{'path'} = Filesys::POSIX::Path->full($path); | |||||||||||||||||||||||||||
| 87 | 0 | $self->{'root'} = $root; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | return $sel; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub enter_filesystem { | ||||||||||||||||||||||||||
|
Subroutine
enter_filesystem: not called CC=4 SCAR=26.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 94 | 0 | $self->{_privileges_reduced}++; | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | return unless ( $self->{_privileges_reduced} == 1 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $self->{_original_uid} = $>; | |||||||||||||||||||||||||||
| 97 | 0 | $self->{_original_gid} = $); | |||||||||||||||||||||||||||
| 98 | 0 | $) = $self->{_gid}; | |||||||||||||||||||||||||||
| 99 | 0 | $> = $self->{_uid}; | |||||||||||||||||||||||||||
| 100 | 1 | ▶ | no warnings 'numeric'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | unless ( $> == $self->{_uid} && int($)) eq int( $self->{_gid} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$> == $self->{"_uid"} and int $) eq int $self->{"_gid"}
MC/DC0%0/2
$> == $self->{"_uid"} and int $) eq int $self->{"_gid"}
$> == $self->{"_uid"}
int $) eq int $self->{"_gid"}
Truth table0%0/3
A and B
A= $> == $self->{"_uid"}B= int $) eq int $self->{"_gid"}
| |||||||||||||||||||||||||||||
| 103 | 0 | Carp::confess("failed to reduce privileges: $!"); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 0 | return; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub exit_filesystem { | ||||||||||||||||||||||||||
|
Subroutine
exit_filesystem: not called CC=4 SCAR=25.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | 0 | $self->{_privileges_reduced}--; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | return unless ( $self->{_privileges_reduced} == 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | $> = $self->{_original_uid}; | |||||||||||||||||||||||||||
| 113 | 0 | $) = $self->{_original_gid}; | |||||||||||||||||||||||||||
| 114 | 1 | ▶ | no warnings 'numeric'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | unless ( $> == $self->{_original_uid} && int($)) eq int( $self->{_original_gid} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$> == $self->{"_original_uid"} and int $) eq int $self->{"_original_gid"}
MC/DC0%0/2
$> == $self->{"_original_uid"} and int $) eq int $self->{"_original_gid"}
$> == $self->{"_original_uid"}
int $) eq int $self->{"_original_gid"}
Truth table0%0/3
A and B
A= $> == $self->{"_original_uid"}B= int $) eq int $self->{"_original_gid"}
| |||||||||||||||||||||||||||||
| 116 | 0 | Carp::confess("failed to restore privileges: $!"); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 0 | return; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | 1; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||