Debian Codegraph @

vendor/wikimedia/shellbox/src/Command/SystemdWrapper.php

raw
1 <?php
2
3 namespace Shellbox\Command;
4
5 use Shellbox\Shellbox;
6 use Shellbox\ShellboxError;
7
8 /**
9 * A wrapper which runs the command via systemd-run
10 */
11 class SystemdWrapper extends Wrapper {
12 /**
13 * Systemd needs to be an outer wrapper since it doesn't directly exec the
14 * binary and doesn't inherit permissions
15 */
16 public const PRIORITY = 60;
17
18 public function __construct() {
19 parent::__construct();
20
21 if ( !function_exists( 'posix_getuid' ) ) {
22 throw new ShellboxError( 'SystemdWrapper requires the posix extension' );
23 }
24 }
25
26 public function wrap( Command $command ) {
27 if ( $command->getDisableSandbox() ) {
28 return;
29 }
30
31 $args = [ '/usr/bin/systemd-run', '--user', '--pipe', '--quiet', '--no-ask-password' ];
32
33 $wd = $command->getWorkingDirectory();
34 if ( $wd !== null ) {
35 $args[] = "-pWorkingDirectory=$wd";
36 }
37
38 $cpu = $command->getCpuTimeLimit();
39 if ( $cpu ) {
40 $args[] = "-pLimitCPU=$cpu";
41 }
42
43 $mem = $command->getMemoryLimit();
44 if ( $mem ) {
45 $args[] = "-pMemoryMax=$mem";
46 $args[] = "-pMemorySwapMax=$mem";
47 }
48
49 $fileSize = $command->getFileSizeLimit();
50 if ( $fileSize ) {
51 $args[] = "-pLimitFSIZE=$fileSize";
52 }
53
54 $paths = $command->getAllowedPaths();
55 if ( $paths ) {
56 $args[] = '-pReadWritePaths=' . $this->makeList( $paths );
57 }
58
59 $paths = $command->getDisallowedPaths();
60 if ( $paths ) {
61 $args[] = '-pTemporaryFileSystem=' . $this->makeList( $paths );
62 }
63
64 if ( $command->getDisableNetwork() ) {
65 $args[] = '-pPrivateNetwork=yes';
66 }
67
68 $disabledSyscalls = $command->getDisabledSyscalls();
69 if ( $disabledSyscalls ) {
70 $args[] = '-pSystemCallFilter=~' . $this->makeList( $disabledSyscalls );
71 }
72
73 if ( $command->getNoNewPrivs() ) {
74 $args[] = '-pNoNewPrivileges=yes';
75 }
76
77 if ( $command->getPrivateUserNamespace() ) {
78 $args[] = '-pPrivateUsers=yes';
79 }
80
81 if ( $command->getPrivateDev() ) {
82 $args[] = '-pPrivateDevices=yes';
83 }
84
85 foreach ( $command->getEnvironment() as $name => $value ) {
86 $args[] = "-E$name=$value";
87 }
88
89 if ( in_array( 'execve', $disabledSyscalls ) ) {
90 $command->unsafeCommand(
91 Shellbox::escape( $args ) . ' ' .
92 $command->getCommandString() );
93 } else {
94 $args[] = '/bin/sh';
95 $args[] = '-c';
96 $args[] = $command->getCommandString();
97 $command->replaceParams( $args );
98 }
99 $command->environment( [
100 'XDG_RUNTIME_DIR' => '/run/user/' . posix_getuid()
101 ] );
102 }
103
104 /**
105 * systemd-run uses space-separated lists as config, an odd convention.
106 * Ensure that the list members do not contain spaces before combining them
107 * into a string.
108 *
109 * @param string[] $paths
110 * @return string
111 * @throws ShellboxError
112 */
113 private function makeList( $paths ) {
114 foreach ( $paths as $path ) {
115 if ( strpos( $path, ' ' ) !== false ) {
116 throw new ShellboxError( 'SystemdWrapper: property list cannot contain a space' );
117 }
118 }
119 return implode( ' ', $paths );
120 }
121
122 public function getPriority() {
123 return self::PRIORITY;
124 }
125 }