Introduces a FirejailCommand class, which can be used to add additional restrictions to a command, for increased security. For now, firejail containment needs to be enabled on a per-command basis. The following restrictions are implemented: * NO_ROOT - disallows any root access, including via setuid binaries * SECCOMP - block dangerous syscalls with seccomp * PRIVATE_DEV - create a private /dev * NO_NETWORK - deny all network access * NO_EXECVE - block the execve syscall A convenient Shell::RESTRICT_DEFAULT is equivalent to NO_ROOT | SECCOMP | PRIVATE_DEV, with the expectation that more restrictions may be added to it in the future. In addition, specific paths can be whitelisted with Command::whitelistPaths(). Any file/directory that isn't whitelisted in that top level directory (e.g. /srv) won't exist inside the firejail. $wgShellRestrictionMethod can be set to false for no restriction system, 'firejail' to explicitly use it, or 'autodetect' to autodetect whatever system is available. In the future the default should be changed to autodetection once firejail is tested more. Bug: T173370 Change-Id: Id74df0dbba40e1e7c07c4368aacffb6eb06a17c5
47 lines
1.2 KiB
PHP
47 lines
1.2 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Shell\Command;
|
|
use MediaWiki\Shell\CommandFactory;
|
|
use MediaWiki\Shell\FirejailCommand;
|
|
use Psr\Log\NullLogger;
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
/**
|
|
* @group Shell
|
|
*/
|
|
class CommandFactoryTest extends PHPUnit_Framework_TestCase {
|
|
/**
|
|
* @covers MediaWiki\Shell\CommandFactory::create
|
|
*/
|
|
public function testCreate() {
|
|
$logger = new NullLogger();
|
|
$cgroup = '/sys/fs/cgroup/memory/mygroup';
|
|
$limits = [
|
|
'filesize' => 1000,
|
|
'memory' => 1000,
|
|
'time' => 30,
|
|
'walltime' => 40,
|
|
];
|
|
|
|
$factory = new CommandFactory( $limits, $cgroup, false );
|
|
$factory->setLogger( $logger );
|
|
$factory->logStderr();
|
|
$command = $factory->create();
|
|
$this->assertInstanceOf( Command::class, $command );
|
|
|
|
$wrapper = TestingAccessWrapper::newFromObject( $command );
|
|
$this->assertSame( $logger, $wrapper->logger );
|
|
$this->assertSame( $cgroup, $wrapper->cgroup );
|
|
$this->assertSame( $limits, $wrapper->limits );
|
|
$this->assertTrue( $wrapper->doLogStderr );
|
|
}
|
|
|
|
/**
|
|
* @covers MediaWiki\Shell\CommandFactory::create
|
|
*/
|
|
public function testFirejailCreate() {
|
|
$factory = new CommandFactory( [], false, 'firejail' );
|
|
$factory->setLogger( new NullLogger() );
|
|
$this->assertInstanceOf( FirejailCommand::class, $factory->create() );
|
|
}
|
|
}
|