1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Shell;
use Exception;
use MediaWiki\ProcOpenError;
use MediaWiki\ShellDisabledError;
use Profiler;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use Wikimedia\AtEase\AtEase;
/**
* Class used for executing shell commands
*
* @since 1.30
*/
class Command {
use LoggerAwareTrait;
/** @var string */
protected $command = '';
/** @var array */
private $limits = [
// seconds
'time' => 180,
// seconds
'walltime' => 180,
// KB
'memory' => 307200,
// KB
'filesize' => 102400,
];
/** @var string[] */
private $env = [];
/** @var string */
private $method;
/** @var string|null */
private $inputString;
/** @var bool */
private $doIncludeStderr = false;
/** @var bool */
private $doLogStderr = false;
/** @var bool */
private $everExecuted = false;
/** @var string|false */
private $cgroup = false;
/**
* Bitfield with restrictions
*
* @var int
*/
protected $restrictions = 0;
/**
* Don't call directly, instead use Shell::command()
*
* @throws ShellDisabledError
*/
public function __construct() {
if ( Shell::isDisabled() ) {
throw new ShellDisabledError();
}
$this->setLogger( new NullLogger() );
}
/**
* Makes sure the programmer didn't forget to execute the command after all
*/
public function __destruct() {
if ( !$this->everExecuted ) {
$context = [ 'command' => $this->command ];
$message = __CLASS__ . " was instantiated, but execute() was never called.";
if ( $this->method ) {
$message .= ' Calling method: {method}.';
$context['method'] = $this->method;
}
$message .= ' Command: {command}';
$this->logger->warning( $message, $context );
}
}
/**
* Adds parameters to the command. All parameters are sanitized via Shell::escape().
* Null values are ignored.
*
* @param string|string[] ...$args
* @return $this
*/
public function params( ...$args ): Command {
if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
// If only one argument has been passed, and that argument is an array,
// treat it as a list of arguments
$args = reset( $args );
}
$this->command = trim( $this->command . ' ' . Shell::escape( $args ) );
return $this;
}
/**
* Adds unsafe parameters to the command. These parameters are NOT sanitized in any way.
* Null values are ignored.
*
* @param string|string[] ...$args
* @return $this
*/
public function unsafeParams( ...$args ): Command {
if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
// If only one argument has been passed, and that argument is an array,
// treat it as a list of arguments
$args = reset( $args );
}
$args = array_filter( $args,
function ( $value ) {
return $value !== null;
}
);
$this->command = trim( $this->command . ' ' . implode( ' ', $args ) );
return $this;
}
/**
* Sets execution limits
*
* @param array $limits Associative array of limits. Keys (all optional):
* filesize (for ulimit -f), memory, time, walltime.
* @return $this
*/
public function limits( array $limits ): Command {
if ( !isset( $limits['walltime'] ) && isset( $limits['time'] ) ) {
// Emulate the behavior of old wfShellExec() where walltime fell back on time
// if the latter was overridden and the former wasn't
$limits['walltime'] = $limits['time'];
}
$this->limits = $limits + $this->limits;
return $this;
}
/**
* Sets environment variables which should be added to the executed command environment
*
* @param string[] $env array of variable name => value
* @return $this
*/
public function environment( array $env ): Command {
$this->env = $env;
return $this;
}
/**
* Sets calling function for profiler. By default, the caller for execute() will be used.
*
* @param string $method
* @return $this
*/
public function profileMethod( string $method ): Command {
$this->method = $method;
return $this;
}
/**
* Sends the provided input to the command.
* When set to null (default), the command will use the standard input.
* @param string|null $inputString
* @return $this
*/
public function input( ?string $inputString ): Command {
$this->inputString = $inputString;
return $this;
}
/**
* Controls whether stderr should be included in stdout, including errors from limit.sh.
* Default: don't include.
*
* @param bool $yesno
* @return $this
*/
public function includeStderr( bool $yesno = true ): Command {
$this->doIncludeStderr = $yesno;
return $this;
}
/**
* When enabled, text sent to stderr will be logged with a level of 'error'.
*
* @param bool $yesno
* @return $this
*/
public function logStderr( bool $yesno = true ): Command {
$this->doLogStderr = $yesno;
return $this;
}
/**
* Sets cgroup for this command
*
* @param string|false $cgroup Absolute file path to the cgroup, or false to not use a cgroup
* @return $this
*/
public function cgroup( $cgroup ): Command {
$this->cgroup = $cgroup;
return $this;
}
/**
* Set restrictions for this request, overwriting any previously set restrictions.
*
* Add the "no network" restriction:
* @code
* $command->restrict( Shell::RESTRICT_DEFAULT | Shell::NO_NETWORK );
* @endcode
*
* Allow LocalSettings.php access:
* @code
* $command->restrict( Shell::RESTRICT_DEFAULT & ~Shell::NO_LOCALSETTINGS );
* @endcode
*
* Disable all restrictions:
* @code
* $command->restrict( Shell::RESTRICT_NONE );
* @endcode
*
* @since 1.31
* @param int $restrictions
* @return $this
*/
public function restrict( int $restrictions ): Command {
$this->restrictions = $restrictions;
return $this;
}
/**
* Bitfield helper on whether a specific restriction is enabled
*
* @param int $restriction
*
* @return bool
*/
protected function hasRestriction( int $restriction ): bool {
return ( $this->restrictions & $restriction ) === $restriction;
}
/**
* If called, only the files/directories that are
* whitelisted will be available to the shell command.
*
* limit.sh will always be whitelisted
*
* @param string[] $paths
*
* @return $this
*/
public function whitelistPaths( array $paths ): Command {
// Default implementation is a no-op
return $this;
}
/**
* String together all the options and build the final command
* to execute
*
* @param string $command Already-escaped command to run
* @return array [ command, whether to use log pipe ]
*/
protected function buildFinalCommand( string $command ): array {
$envcmd = '';
foreach ( $this->env as $k => $v ) {
if ( wfIsWindows() ) {
/* Surrounding a set in quotes (method used by wfEscapeShellArg) makes the quotes themselves
* appear in the environment variable, so we must use carat escaping as documented in
* https://technet.microsoft.com/en-us/library/cc723564.aspx
* Note however that the quote isn't listed there, but is needed, and the parentheses
* are listed there but doesn't appear to need it.
*/
$envcmd .= "set $k=" . preg_replace( '/([&|()<>^"])/', '^\\1', $v ) . '&& ';
} else {
/* Assume this is a POSIX shell, thus required to accept variable assignments before the command
* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_01
*/
$envcmd .= "$k=" . escapeshellarg( $v ) . ' ';
}
}
$useLogPipe = false;
$cmd = $envcmd . trim( $command );
if ( is_executable( '/bin/bash' ) ) {
$time = intval( $this->limits['time'] );
$wallTime = intval( $this->limits['walltime'] );
$mem = intval( $this->limits['memory'] );
$filesize = intval( $this->limits['filesize'] );
if ( $time > 0 || $mem > 0 || $filesize > 0 || $wallTime > 0 ) {
$cmd = '/bin/bash ' . escapeshellarg( __DIR__ . '/limit.sh' ) . ' ' .
escapeshellarg( $cmd ) . ' ' .
escapeshellarg(
"MW_INCLUDE_STDERR=" . ( $this->doIncludeStderr ? '1' : '' ) . ';' .
"MW_CPU_LIMIT=$time; " .
'MW_CGROUP=' . escapeshellarg( $this->cgroup ) . '; ' .
"MW_MEM_LIMIT=$mem; " .
"MW_FILE_SIZE_LIMIT=$filesize; " .
"MW_WALL_CLOCK_LIMIT=$wallTime; " .
"MW_USE_LOG_PIPE=yes"
);
$useLogPipe = true;
}
}
if ( !$useLogPipe && $this->doIncludeStderr ) {
$cmd .= ' 2>&1';
}
if ( wfIsWindows() ) {
$cmd = 'cmd.exe /c "' . $cmd . '"';
}
return [ $cmd, $useLogPipe ];
}
/**
* Executes command. Afterwards, getExitCode() and getOutput() can be used to access execution
* results.
*
* @return Result
* @throws Exception
* @throws ProcOpenError
* @throws ShellDisabledError
*/
public function execute(): Result {
$this->everExecuted = true;
$profileMethod = $this->method ?: wfGetCaller();
list( $cmd, $useLogPipe ) = $this->buildFinalCommand( $this->command );
$this->logger->debug( __METHOD__ . ": $cmd" );
// Don't try to execute commands that exceed Linux's MAX_ARG_STRLEN.
// Other platforms may be more accomodating, but we don't want to be
// accomodating, because very long commands probably include user
// input. See T129506.
if ( strlen( $cmd ) > SHELL_MAX_ARG_STRLEN ) {
throw new Exception( __METHOD__ .
'(): total length of $cmd must not exceed SHELL_MAX_ARG_STRLEN' );
}
$desc = [
0 => $this->inputString === null ? [ 'file', 'php://stdin', 'r' ] : [ 'pipe', 'r' ],
1 => [ 'pipe', 'w' ],
2 => [ 'pipe', 'w' ],
];
if ( $useLogPipe ) {
$desc[3] = [ 'pipe', 'w' ];
}
$pipes = null;
$scoped = Profiler::instance()->scopedProfileIn( __FUNCTION__ . '-' . $profileMethod );
$proc = null;
if ( wfIsWindows() ) {
// Windows Shell bypassed, but command run is "cmd.exe /C "{$cmd}"
// This solves some shell parsing issues, see T207248
$proc = proc_open( $cmd, $desc, $pipes, null, null, [ 'bypass_shell' => true ] );
} else {
$proc = proc_open( $cmd, $desc, $pipes );
}
if ( !$proc ) {
$this->logger->error( "proc_open() failed: {command}", [ 'command' => $cmd ] );
throw new ProcOpenError();
}
$buffers = [
0 => $this->inputString, // input
1 => '', // stdout
2 => null, // stderr
3 => '', // log
];
$emptyArray = [];
$status = false;
$logMsg = false;
/* According to the documentation, it is possible for stream_select()
* to fail due to EINTR. I haven't managed to induce this in testing
* despite sending various signals. If it did happen, the error
* message would take the form:
*
* stream_select(): unable to select [4]: Interrupted system call (max_fd=5)
*
* where [4] is the value of the macro EINTR and "Interrupted system
* call" is string which according to the Linux manual is "possibly"
* localised according to LC_MESSAGES.
*/
$eintr = defined( 'SOCKET_EINTR' ) ? SOCKET_EINTR : 4;
$eintrMessage = "stream_select(): unable to select [$eintr]";
/* The select(2) system call only guarantees a "sufficiently small write"
* can be made without blocking. And on Linux the read might block too
* in certain cases, although I don't know if any of them can occur here.
* Regardless, set all the pipes to non-blocking to avoid T184171.
*/
foreach ( $pipes as $pipe ) {
stream_set_blocking( $pipe, false );
}
$running = true;
$timeout = null;
$numReadyPipes = 0;
while ( $pipes && ( $running === true || $numReadyPipes !== 0 ) ) {
if ( $running ) {
$status = proc_get_status( $proc );
// If the process has terminated, switch to nonblocking selects
// for getting any data still waiting to be read.
if ( !$status['running'] ) {
$running = false;
$timeout = 0;
}
}
error_clear_last();
$readPipes = array_filter( $pipes, function ( $fd ) use ( $desc ) {
return $desc[$fd][0] === 'pipe' && $desc[$fd][1] === 'r';
}, ARRAY_FILTER_USE_KEY );
$writePipes = array_filter( $pipes, function ( $fd ) use ( $desc ) {
return $desc[$fd][0] === 'pipe' && $desc[$fd][1] === 'w';
}, ARRAY_FILTER_USE_KEY );
// stream_select parameter names are from the POV of us being able to do the operation;
// proc_open desriptor types are from the POV of the process doing it.
// So $writePipes is passed as the $read parameter and $readPipes as $write.
AtEase::suppressWarnings();
$numReadyPipes = stream_select( $writePipes, $readPipes, $emptyArray, $timeout );
AtEase::restoreWarnings();
if ( $numReadyPipes === false ) {
$error = error_get_last();
if ( strncmp( $error['message'], $eintrMessage, strlen( $eintrMessage ) ) == 0 ) {
continue;
} else {
trigger_error( $error['message'], E_USER_WARNING );
$logMsg = $error['message'];
break;
}
}
foreach ( $writePipes + $readPipes as $fd => $pipe ) {
// True if a pipe is unblocked for us to write into, false if for reading from
$isWrite = array_key_exists( $fd, $readPipes );
if ( $isWrite ) {
// Don't bother writing if the buffer is empty
if ( $buffers[$fd] === '' ) {
fclose( $pipes[$fd] );
unset( $pipes[$fd] );
continue;
}
$res = fwrite( $pipe, $buffers[$fd], 65536 );
} else {
$res = fread( $pipe, 65536 );
}
if ( $res === false ) {
$logMsg = 'Error ' . ( $isWrite ? 'writing to' : 'reading from' ) . ' pipe';
break 2;
}
if ( $res === '' || $res === 0 ) {
// End of file?
if ( feof( $pipe ) ) {
fclose( $pipes[$fd] );
unset( $pipes[$fd] );
}
} elseif ( $isWrite ) {
$buffers[$fd] = (string)substr( $buffers[$fd], $res );
if ( $buffers[$fd] === '' ) {
fclose( $pipes[$fd] );
unset( $pipes[$fd] );
}
} else {
$buffers[$fd] .= $res;
if ( $fd === 3 && strpos( $res, "\n" ) !== false ) {
// For the log FD, every line is a separate log entry.
$lines = explode( "\n", $buffers[3] );
$buffers[3] = array_pop( $lines );
foreach ( $lines as $line ) {
$this->logger->info( $line );
}
}
}
}
}
foreach ( $pipes as $pipe ) {
fclose( $pipe );
}
// Use the status previously collected if possible, since proc_get_status()
// just calls waitpid() which will not return anything useful the second time.
if ( $running ) {
$status = proc_get_status( $proc );
}
if ( $logMsg !== false ) {
// Read/select error
$retval = -1;
proc_close( $proc );
} elseif ( $status['signaled'] ) {
$logMsg = "Exited with signal {$status['termsig']}";
$retval = 128 + $status['termsig'];
proc_close( $proc );
} else {
if ( $status['running'] ) {
$retval = proc_close( $proc );
} else {
$retval = $status['exitcode'];
proc_close( $proc );
}
if ( $retval == 127 ) {
$logMsg = "Possibly missing executable file";
} elseif ( $retval >= 129 && $retval <= 192 ) {
$logMsg = "Probably exited with signal " . ( $retval - 128 );
}
}
if ( $logMsg !== false ) {
$this->logger->warning( "$logMsg: {command}", [ 'command' => $cmd ] );
}
// @phan-suppress-next-line PhanImpossibleCondition
if ( $buffers[2] && $this->doLogStderr ) {
$this->logger->error( "Error running {command}: {error}", [
'command' => $cmd,
'error' => $buffers[2],
'exitcode' => $retval,
'exception' => new Exception( 'Shell error' ),
] );
}
return new Result( $retval, $buffers[1], $buffers[2] );
}
/**
* Returns the final command line before environment/limiting, etc are applied.
* Use string conversion only for debugging, don't try to pass this to
* some other execution medium.
*
* @return string
*/
public function __toString(): string {
return "#Command: {$this->command}";
}
}
|