Debian Codegraph @

vendor/wikimedia/shellbox/src/Command/FirejailWrapper.php

raw
1 <?php
2
3 namespace Shellbox\Command;
4
5 use Shellbox\ShellboxError;
6
7 /**
8 * A wrapper that restricts the command using firejail
9 */
10 class FirejailWrapper extends Wrapper {
11 /**
12 * Firejail is a setuid-root executable which naturally goes inside systemd
13 * but outside BashWrapper, since it inherits and preserves most aspects of
14 * the system.
15 */
16 public const PRIORITY = 40;
17
18 /**
19 * @var string The path to firejail
20 */
21 private $binaryPath;
22
23 /**
24 * @var string The path to the profile file
25 */
26 private $profilePath;
27
28 /**
29 * @param string $binaryPath The path to firejail
30 * @param string $profilePath The path to the profile file
31 */
32 public function __construct( $binaryPath, $profilePath ) {
33 parent::__construct();
34 $this->binaryPath = $binaryPath;
35 $this->profilePath = $profilePath;
36 }
37
38 public function wrap( Command $command ) {
39 // If there are no restrictions, don't use firejail
40 if ( $command->getDisableSandbox() ) {
41 $splitCommand = explode( ' ', $command->getCommandString(), 2 );
42 $this->logger->debug(
43 "firejail: Command {$splitCommand[0]} {params} has no restrictions",
44 [ 'params' => $splitCommand[1] ?? '' ]
45 );
46 return;
47 }
48
49 // quiet has to come first to prevent firejail from adding
50 // any output.
51 $cmd = [ $this->binaryPath, '--quiet' ];
52 // Use a profile that allows people to add local overrides
53 // if their system is setup in an incompatible manner. Also it
54 // prevents any default profiles from running.
55 // FIXME: Doesn't actually override command-line switches?
56 $cmd[] = '--profile=' . $this->profilePath;
57
58 foreach ( $command->getAllowedPaths() as $path ) {
59 if ( $path === '/home' ) {
60 $cmd[] = '--allusers';
61 } else {
62 $cmd[] = "--whitelist={$path}";
63 }
64 }
65
66 foreach ( $command->getDisallowedPaths() as $path ) {
67 $cmd[] = '--blacklist=' . $path;
68 }
69
70 if ( $command->getPrivateUserNamespace() ) {
71 $cmd[] = '--noroot';
72 }
73
74 $extraSeccomp = $command->getDisabledSyscalls();
75 $useSeccomp = $extraSeccomp || $command->getFirejailDefaultSeccomp();
76
77 if ( in_array( 'execve', $extraSeccomp ) ) {
78 // Running the command in the shell won't work without the execve
79 // syscall, so split the command string into literal arguments and
80 // pass those to firejail for direct execution.
81 // Firejail 0.9.72 or later must be used for this mode to work.
82 $argv = $command->getSyntaxInfo()->getLiteralArgv();
83 if ( $argv === null ) {
84 throw new ShellboxError(
85 "The command contained non-literal shell components but " .
86 "seccomp=execve was requested"
87 );
88 }
89 } else {
90 // Wrap the whole command in a shell (T353194)
91 $argv = [ '/bin/sh', '-c', '--', $command->getCommandString() ];
92 }
93
94 if ( $useSeccomp ) {
95 $seccomp = '--seccomp';
96 if ( $extraSeccomp ) {
97 // The "@default" seccomp group will always be enabled
98 $seccomp .= '=' . implode( ',', $extraSeccomp );
99 }
100 $cmd[] = $seccomp;
101 }
102
103 if ( $command->getPrivateDev() ) {
104 $cmd[] = '--private-dev';
105 }
106
107 if ( $command->getDisableNetwork() ) {
108 $cmd[] = '--net=none';
109 }
110
111 foreach ( $command->getEnvironment() as $name => $value ) {
112 if ( (string)$value !== '' ) {
113 $cmd[] = "--env=$name=$value";
114 }
115 }
116
117 // Prefix the firejail command in front of the wanted command
118 $command->replaceParams( array_merge( $cmd, $argv ) );
119 }
120
121 public function getPriority() {
122 return self::PRIORITY;
123 }
124 }