Command.php 16 KB

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