| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Shell; |
| 22 |
|
| 23 |
use ExecutableFinder; |
| 24 |
use Psr\Log\LoggerAwareTrait; |
| 25 |
use Psr\Log\NullLogger; |
| 26 |
use Shellbox\Command\BoxedCommand; |
| 27 |
use Shellbox\Command\RemoteBoxedExecutor; |
| 28 |
use Shellbox\Shellbox; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class CommandFactory { |
| 36 |
use LoggerAwareTrait; |
| 37 |
|
| 38 |
|
| 39 |
private $limits; |
| 40 |
|
| 41 |
|
| 42 |
private $cgroup; |
| 43 |
|
| 44 |
|
| 45 |
private $doLogStderr = false; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private $restrictionMethod; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private $firejail; |
| 56 |
|
| 57 |
|
| 58 |
private $useAllUsers; |
| 59 |
|
| 60 |
|
| 61 |
private $shellboxClientFactory; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function __construct( ShellboxClientFactory $shellboxClientFactory, |
| 70 |
array $limits, $cgroup, $restrictionMethod |
| 71 |
) { |
| 72 |
$this->shellboxClientFactory = $shellboxClientFactory; |
| 73 |
$this->limits = $limits; |
| 74 |
$this->cgroup = $cgroup; |
| 75 |
if ( $restrictionMethod === 'autodetect' ) { |
| 76 |
|
| 77 |
if ( PHP_OS === 'Linux' && $this->findFirejail() ) { |
| 78 |
$this->restrictionMethod = 'firejail'; |
| 79 |
} else { |
| 80 |
$this->restrictionMethod = false; |
| 81 |
} |
| 82 |
} else { |
| 83 |
$this->restrictionMethod = $restrictionMethod; |
| 84 |
} |
| 85 |
$this->setLogger( new NullLogger() ); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
protected function findFirejail() { |
| 92 |
if ( $this->firejail === null ) { |
| 93 |
$this->firejail = ExecutableFinder::findInDefaultPaths( 'firejail' ); |
| 94 |
} |
| 95 |
|
| 96 |
return $this->firejail; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function logStderr( bool $yesno = true ): void { |
| 106 |
$this->doLogStderr = $yesno; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
private function getLocalShellboxOptions() { |
| 117 |
$options = [ |
| 118 |
'tempDir' => wfTempDir(), |
| 119 |
'useBashWrapper' => file_exists( '/bin/bash' ), |
| 120 |
'cgroup' => $this->cgroup |
| 121 |
]; |
| 122 |
if ( $this->restrictionMethod === 'firejail' ) { |
| 123 |
$firejailPath = $this->findFirejail(); |
| 124 |
if ( !$firejailPath ) { |
| 125 |
throw new \RuntimeException( 'firejail is enabled, but cannot be found' ); |
| 126 |
} |
| 127 |
$options['useFirejail'] = true; |
| 128 |
$options['firejailPath'] = $firejailPath; |
| 129 |
$options['firejailProfile'] = __DIR__ . '/firejail.profile'; |
| 130 |
} |
| 131 |
return $options; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function create(): Command { |
| 140 |
$allUsers = false; |
| 141 |
if ( $this->restrictionMethod === 'firejail' ) { |
| 142 |
if ( $this->useAllUsers === null ) { |
| 143 |
global $IP; |
| 144 |
|
| 145 |
|
| 146 |
$realIP = realpath( $IP ); |
| 147 |
$currentUser = posix_getpwuid( posix_geteuid() ); |
| 148 |
$this->useAllUsers = str_starts_with( $realIP, '/home/' ) |
| 149 |
&& !str_starts_with( $realIP, $currentUser['dir'] ); |
| 150 |
if ( $this->useAllUsers ) { |
| 151 |
$this->logger->warning( 'firejail: MediaWiki is located ' . |
| 152 |
'in a home directory that does not belong to the ' . |
| 153 |
'current user, so allowing access to all home ' . |
| 154 |
'directories (--allusers)' ); |
| 155 |
} |
| 156 |
} |
| 157 |
$allUsers = $this->useAllUsers; |
| 158 |
} |
| 159 |
$executor = Shellbox::createUnboxedExecutor( |
| 160 |
$this->getLocalShellboxOptions(), $this->logger ); |
| 161 |
|
| 162 |
$command = new Command( $executor ); |
| 163 |
$command->setLogger( $this->logger ); |
| 164 |
if ( $allUsers ) { |
| 165 |
$command->allowPath( '/home' ); |
| 166 |
} |
| 167 |
return $command |
| 168 |
->limits( $this->limits ) |
| 169 |
->logStderr( $this->doLogStderr ); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function createBoxed( ?string $service = null, $wallTimeLimit = null ): BoxedCommand { |
| 183 |
$wallTimeLimit ??= $this->limits['walltime']; |
| 184 |
if ( $this->shellboxClientFactory->isEnabled( $service ) ) { |
| 185 |
$client = $this->shellboxClientFactory->getClient( [ |
| 186 |
'timeout' => $wallTimeLimit + 1, |
| 187 |
'service' => $service, |
| 188 |
] ); |
| 189 |
$executor = new RemoteBoxedExecutor( $client ); |
| 190 |
$executor->setLogger( $this->logger ); |
| 191 |
} else { |
| 192 |
$executor = Shellbox::createBoxedExecutor( |
| 193 |
$this->getLocalShellboxOptions(), |
| 194 |
$this->logger ); |
| 195 |
} |
| 196 |
return $executor->createCommand() |
| 197 |
->cpuTimeLimit( $this->limits['time'] ) |
| 198 |
->wallTimeLimit( $wallTimeLimit ) |
| 199 |
->memoryLimit( $this->limits['memory'] * 1024 ) |
| 200 |
->fileSizeLimit( $this->limits['filesize'] * 1024 ) |
| 201 |
->logStderr( $this->doLogStderr ); |
| 202 |
} |
| 203 |
} |