FirejailCommand.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Copyright (C) 2017 Kunal Mehta <legoktm@member.fsf.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. */
  20. namespace MediaWiki\Shell;
  21. use RuntimeException;
  22. /**
  23. * Restricts execution of shell commands using firejail
  24. *
  25. * @see https://firejail.wordpress.com/
  26. * @since 1.31
  27. */
  28. class FirejailCommand extends Command {
  29. /**
  30. * @var string Path to firejail
  31. */
  32. private $firejail;
  33. /**
  34. * @var string[]
  35. */
  36. private $whitelistedPaths = [];
  37. /**
  38. * @param string $firejail Path to firejail
  39. */
  40. public function __construct( $firejail ) {
  41. parent::__construct();
  42. $this->firejail = $firejail;
  43. }
  44. /**
  45. * @inheritDoc
  46. */
  47. public function whitelistPaths( array $paths ): Command {
  48. $this->whitelistedPaths = array_merge( $this->whitelistedPaths, $paths );
  49. return $this;
  50. }
  51. /**
  52. * @inheritDoc
  53. */
  54. protected function buildFinalCommand( $command ) {
  55. // If there are no restrictions, don't use firejail
  56. if ( $this->restrictions === 0 ) {
  57. $splitCommand = explode( ' ', $command, 2 );
  58. $this->logger->debug(
  59. "firejail: Command {$splitCommand[0]} {params} has no restrictions",
  60. [ 'params' => $splitCommand[1] ?? '' ]
  61. );
  62. return parent::buildFinalCommand( $command );
  63. }
  64. if ( $this->firejail === false ) {
  65. throw new RuntimeException( 'firejail is enabled, but cannot be found' );
  66. }
  67. // quiet has to come first to prevent firejail from adding
  68. // any output.
  69. $cmd = [ $this->firejail, '--quiet' ];
  70. // Use a profile that allows people to add local overrides
  71. // if their system is setup in an incompatible manner. Also it
  72. // prevents any default profiles from running.
  73. // FIXME: Doesn't actually override command-line switches?
  74. $cmd[] = '--profile=' . __DIR__ . '/firejail.profile';
  75. // By default firejail hides all other user directories, so if
  76. // MediaWiki is inside a home directory (/home) but not the
  77. // current user's home directory, pass --allusers to whitelist
  78. // the home directories again.
  79. static $useAllUsers = null;
  80. if ( $useAllUsers === null ) {
  81. global $IP;
  82. // In case people are doing funny things with symlinks
  83. // or relative paths, resolve them all.
  84. $realIP = realpath( $IP );
  85. $currentUser = posix_getpwuid( posix_geteuid() );
  86. $useAllUsers = ( strpos( $realIP, '/home/' ) === 0 )
  87. && ( strpos( $realIP, $currentUser['dir'] ) !== 0 );
  88. if ( $useAllUsers ) {
  89. $this->logger->warning( 'firejail: MediaWiki is located ' .
  90. 'in a home directory that does not belong to the ' .
  91. 'current user, so allowing access to all home ' .
  92. 'directories (--allusers)' );
  93. }
  94. }
  95. if ( $useAllUsers ) {
  96. $cmd[] = '--allusers';
  97. }
  98. if ( $this->whitelistedPaths ) {
  99. // Always whitelist limit.sh
  100. $cmd[] = '--whitelist=' . __DIR__ . '/limit.sh';
  101. foreach ( $this->whitelistedPaths as $whitelistedPath ) {
  102. $cmd[] = "--whitelist={$whitelistedPath}";
  103. }
  104. }
  105. if ( $this->hasRestriction( Shell::NO_LOCALSETTINGS ) ) {
  106. $cmd[] = '--blacklist=' . realpath( MW_CONFIG_FILE );
  107. }
  108. if ( $this->hasRestriction( Shell::NO_ROOT ) ) {
  109. $cmd[] = '--noroot';
  110. }
  111. $useSeccomp = $this->hasRestriction( Shell::SECCOMP );
  112. $extraSeccomp = [];
  113. if ( $this->hasRestriction( Shell::NO_EXECVE ) ) {
  114. $extraSeccomp[] = 'execve';
  115. // Normally firejail will run commands in a bash shell,
  116. // but that won't work if we ban the execve syscall, so
  117. // run the command without a shell.
  118. $cmd[] = '--shell=none';
  119. }
  120. if ( $useSeccomp ) {
  121. $seccomp = '--seccomp';
  122. if ( $extraSeccomp ) {
  123. // The "@default" seccomp group will always be enabled
  124. $seccomp .= '=' . implode( ',', $extraSeccomp );
  125. }
  126. $cmd[] = $seccomp;
  127. }
  128. if ( $this->hasRestriction( Shell::PRIVATE_DEV ) ) {
  129. $cmd[] = '--private-dev';
  130. }
  131. if ( $this->hasRestriction( Shell::NO_NETWORK ) ) {
  132. $cmd[] = '--net=none';
  133. }
  134. $builtCmd = implode( ' ', $cmd );
  135. // Prefix the firejail command in front of the wanted command
  136. return parent::buildFinalCommand( "$builtCmd -- {$command}" );
  137. }
  138. }