lib/Mojo/IOLoop/ReadWriteProcess/Namespace.pm

filter: Statementstmt 85.0% 57 / 67 Branchbran 42.5% 17 / 40 Conditioncond 38.4% 5 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 65.4% 93 / 142 CC 26 SCAR 28.8
CC
26
Cov
66%
_get_unshare_syscall
43.9
_get_mount_syscall
8.6
isolate
4.8
CRAP 17.7
« lib/Mojo/IOLoop/ReadWriteProcess/Session.pm ↑ Summary lib/Mojo/IOLoop/ReadWriteProcess/Shared/Lock.pm »
1 package Mojo::IOLoop::ReadWriteProcess::Namespace;
2 13 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 CLONE_ALL => 0,
9 CLONE_NEWNS => 0x00020000,
10 CLONE_NEWIPC => 0x08000000,
11 CLONE_NEWNET => 0x40000000,
12 CLONE_NEWUTS => 0x04000000,
13 CLONE_NEWPID => 0x20000000,
14 CLONE_NEWUSER => 0x10000000,
15 CLONE_NEWCGROUP => 0x02000000,
16 MS_REC => 0x4000,
17 MS_PRIVATE => 1 << 18,
18 MS_NOSUID => 2,
19 MS_NOEXEC => 8,
20 MS_NODEV => 4,
21 13 };
22  
23 13 our @EXPORT_OK = (
24 qw(CLONE_ALL CLONE_NEWNS CLONE_NEWIPC CLONE_NEWUTS),
25 qw(CLONE_NEWNET CLONE_NEWPID CLONE_NEWUSER CLONE_NEWCGROUP),
26 qw(MS_REC MS_PRIVATE MS_NOSUID MS_NOEXEC MS_NODEV)
27 );
28 13 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 3 ▶sub _get_unshare_syscall {
Subroutine _get_unshare_syscall: called CC=12 SCAR=37.8
31  
32 3 ▶ confess "Only Linux is supported" unless $^O eq 'linux';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "linux" 0 3 3
33  
34 3 my $machine = (POSIX::uname())[4];
35 3 ▶ die "Could not get machine type" unless $machine;
Branch: 1 branch
BranchTrueFalseTotal
unless $machine 0 3 3
36  
37 # if we're running on an x86_64 kernel, but a 32-bit process,
38 # we need to use the i386 syscall numbers.
39 3 ▶ $machine = "i386" if ($machine eq "x86_64" && $Config{ptrsize} == 4);
Branch: 1 branch
BranchTrueFalseTotal
if $machine eq "x86_64" and $Mojo::IOLoop::ReadWriteProcess::Namespace::Config{"ptrsize"} == 4 0 3 3
Condition33%1/3
$machine eq "x86_64" and $Mojo::IOLoop::ReadWriteProcess::Namespace::Config{"ptrsize"} == 4
!ll&&!rl&&r
030
MC/DC0%0/2
$machine eq "x86_64" and $Mojo::IOLoop::ReadWriteProcess::Namespace::Config{"ptrsize"} == 4
$machine eq "x86_64" $Mojo::IOLoop::ReadWriteProcess::Namespace::Config{"ptrsize"} == 4
Truth table33%1/3
A and B
A= $machine eq "x86_64"B= $Mojo::IOLoop::ReadWriteProcess::Namespace::Config{"ptrsize"} == 4
ABresult
0X0
100
111
40  
41 3 ▶ my $prctl_call
Branch: 5 branches
BranchTrueFalseTotal
$machine =~ /^i[3456]86|^blackfin|cris|frv|h8300|m32r|m68k|microblaze|mn10300|sh|parisc$/u ? : 0 3 3
$machine eq 's390' ? : 0 3 3
$machine eq 'x86_64' ? : 3 0 3
$machine eq 'ppc' ? : 0 0 0
$machine eq 'ia64' ? : 0 0 0
42 = $machine
43 =~ /^i[3456]86|^blackfin|cris|frv|h8300|m32r|m68k|microblaze|mn10300|sh|parisc$/
44 ? 310
45 : $machine eq "s390" ? 303
46  
47 : $machine eq "x86_64" ? 272
48 : $machine eq "ppc" ? 282
49 : $machine eq "ia64" ? 1296
50 : undef;
51  
52 3 ▶ unless (defined $prctl_call) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prctl_call) 0 3 3
53 delete @INC{
54 0 qw<syscall.ph asm/unistd.ph bits/syscall.ph _h2ph_pre.ph
55 sys/syscall.ph>
56 };
57 0 my $rv = eval { require 'syscall.ph'; 1 } ## no critic
58 0 ▶ or eval { require 'sys/syscall.ph'; 1 }; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
my $rv = eval { do { require "syscall.ph"; 1 } } or eval { do { require "sys/syscall.ph"; '???' } } 0 0 0
59  
60 0 $prctl_call = eval { &SYS_unshare; };
61 }
62 3 return $prctl_call;
63 }
64  
65 2 ▶sub _get_mount_syscall {
Subroutine _get_mount_syscall: called CC=7 SCAR=21.5
66  
67 2 ▶ confess "Only Linux is supported" unless $^O eq 'linux';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "linux" 0 2 2
68  
69 2 my $machine = (POSIX::uname())[4];
70 2 ▶ die "Could not get machine type" unless $machine;
Branch: 1 branch
BranchTrueFalseTotal
unless $machine 0 2 2
71  
72 # if we're running on an x86_64 kernel, but a 32-bit process,
73 # we need to use the i386 syscall numbers.
74 2 ▶ $machine = "i386" if ($machine eq "x86_64" && $Config{ptrsize} == 4);
Branch: 1 branch
BranchTrueFalseTotal
if $machine eq "x86_64" and $Mojo::IOLoop::ReadWriteProcess::Namespace::Config{"ptrsize"} == 4 0 2 2
Condition33%1/3
$machine eq "x86_64" and $Mojo::IOLoop::ReadWriteProcess::Namespace::Config{"ptrsize"} == 4
!ll&&!rl&&r
020
MC/DC0%0/2
$machine eq "x86_64" and $Mojo::IOLoop::ReadWriteProcess::Namespace::Config{"ptrsize"} == 4
$machine eq "x86_64" $Mojo::IOLoop::ReadWriteProcess::Namespace::Config{"ptrsize"} == 4
Truth table33%1/3
A and B
A= $machine eq "x86_64"B= $Mojo::IOLoop::ReadWriteProcess::Namespace::Config{"ptrsize"} == 4
ABresult
0X0
100
111
75  
76 2 my $prctl_call;
77  
78 # $machine
79 # =~ /^i[3456]86|^blackfin|cris|frv|h8300|m32r|m68k|microblaze|mn10300|sh|parisc$/
80 # ? 310
81 # : $machine eq "s390" ? 303
82 #
83 # : $machine eq "x86_64" ? 272
84 # : $machine eq "ppc" ? 282
85 # : $machine eq "ia64" ? 1296
86 # :
87  
88  
89 2 ▶ unless (defined $prctl_call) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prctl_call) 2 0 2
90 delete @INC{
91 2 qw<syscall.ph asm/unistd.ph bits/syscall.ph _h2ph_pre.ph
92 sys/syscall.ph>
93 };
94 0 my $rv = eval { require 'syscall.ph'; 1 } ## no critic
95 0 ▶ or eval { require 'sys/syscall.ph'; 1 }; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
my $rv = eval { do { require "syscall.ph"; 1 } } or eval { do { require "sys/syscall.ph"; '???' } } 0 2 2
96  
97 2 $prctl_call = eval { &SYS_mount; };
98 }
99 2 return $prctl_call;
100 }
101  
102 2 ▶sub mount {
Subroutine mount: called CC=4 SCAR=14.3
Pod documented
103 2 my ($self, $arg1, $arg2, $arg3, $opts) = (@_);
104 2 ▶ $arg3 //= 0;
Condition50%1/2
$arg3 //= 0
l!l
20
MC/DC0%0/1
$arg3 //= 0
$arg3
Truth table50%1/2
A
A= $arg3
Aresult
11
00
105 2 local $!;
106 2 my $ret
107 = syscall(_get_mount_syscall(), my $s = $arg1, my $t = $arg2, $arg3, $opts,
108 0);
109  
110 2 ▶ warn "mount is unavailable on this platform." if $!{EINVAL};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINVAL"} 0 2 2
111 2 ▶ warn "Mount failed! $!" if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 2 0 2
112 2 return $ret;
113 }
114  
115 3 ▶sub unshare {
Subroutine unshare: called CC=3 SCAR=11.2
Pod documented
116 3 my ($self, $opts) = @_;
117 3 local $!;
118 3 my $ret = syscall(_get_unshare_syscall(), $opts, 0, 0);
119  
120 3 ▶ warn "unshare is unavailable on this platform." if $!{EINVAL};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINVAL"} 0 3 3
121 3 ▶ warn "Unshare failed! $!" if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 3 0 3
122 3 return $ret;
123 }
124  
125 1 ▶sub isolate {
Subroutine isolate: called CC=4 SCAR=15.6
Pod documented
126 1 my ($self, $procdir) = shift;
127 1 ▶ $procdir //= "/proc";
Condition50%1/2
$procdir //= "/proc"
l!l
01
MC/DC0%0/1
$procdir //= "/proc"
$procdir
Truth table50%1/2
A
A= $procdir
Aresult
11
00
128 1 $self->mount("none", "/", 0, MS_REC | MS_PRIVATE);
129 1 ▶ warn "Failed isolating proc"
Branch: 1 branch
BranchTrueFalseTotal
if $self->mount("none", $procdir, 0, 278528) != 0 or $self->mount("proc", $procdir, "proc", 14) != 0 1 0 1
Condition33%1/3
$self->mount("none", $procdir, 0, 278528) != 0 or $self->mount("proc", $procdir, "proc", 14) != 0
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->mount("none", $procdir, 0, 278528) != 0 or $self->mount("proc", $procdir, "proc", 14) != 0
$self->mount("none", $procdir, 0, 278528) != 0 $self->mount("proc", $procdir, "proc", 14) != 0
Truth table33%1/3
A or B
A= $self->mount("none", $procdir, 0, 278528) != 0B= $self->mount("proc", $procdir, "proc", 14) != 0
ABresult
1X1
011
000
130 if $self->mount("none", $procdir, 0, MS_REC | MS_PRIVATE) != 0
131 || $self->mount("proc", $procdir, "proc", MS_NOSUID | MS_NOEXEC | MS_NODEV)
132 != 0;
133 }
134  
135 =encoding utf-8
136  
137 =head1 NAME
138  
139 Mojo::IOLoop::ReadWriteProcess::Namespace - Namespace object for Mojo::IOLoop::ReadWriteProcess.
140  
141 =head1 SYNOPSIS
142  
143 use Mojo::IOLoop::ReadWriteProcess::Namespace qw(CLONE_ALL);
144  
145 my $ns = Mojo::IOLoop::ReadWriteProcess::Namespace->new();
146  
147 $ns->unshare(CLONE_ALL);
148 $ns->mount("proc", "/proc", "proc");
149 $ns->isolate();
150  
151 =head1 METHODS
152  
153 L<Mojo::IOLoop::ReadWriteProcess::Namespace> inherits all methods from L<Mojo::Base> and implements
154 the following new ones.
155  
156 =head2 unshare
157  
158 use Mojo::IOLoop::ReadWriteProcess::Namespace qw(CLONE_ALL);
159 my $ns = Mojo::IOLoop::ReadWriteProcess::Namespace->new();
160 $ns->unshare(CLONE_ALL);
161  
162 Wrapper around the unshare syscall, accepts the same arguments,
163 constants can be exported from L<Mojo::IOLoop::ReadWriteProcess::Namespace>.
164  
165 =head2 mount
166  
167 my $ns = Mojo::IOLoop::ReadWriteProcess::Namespace->new();
168 $ns->mount("proc", "/proc", "proc");
169  
170 Wrapper around the mount syscall, accepts the same arguments.
171  
172 =head2 isolate
173  
174 my $ns = Mojo::IOLoop::ReadWriteProcess::Namespace->new();
175 $ns->isolate();
176  
177 Mount appropriately /proc to achieve process isolation during process containment, see L<Mojo::IOLoop::ReadWriteProcess::Container>.
178  
179 =head1 LICENSE
180  
181 Copyright (C) Ettore Di Giacinto.
182  
183 This library is free software; you can redistribute it and/or modify
184 it under the same terms as Perl itself.
185  
186 =head1 AUTHOR
187  
188 Ettore Di Giacinto E<lt>edigiacinto@suse.comE<gt>
189  
190 =cut
191  
192  
193 13 1;
« lib/Mojo/IOLoop/ReadWriteProcess/Session.pm ↑ Summary lib/Mojo/IOLoop/ReadWriteProcess/Shared/Lock.pm »