shell: Optionally restrict commands' access with firejail
[lhc/web/wiklou.git] / includes / shell / Shell.php
1 <?php
2 /**
3 * Class used for executing shell commands
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 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Shell;
24
25 use MediaWiki\MediaWikiServices;
26
27 /**
28 * Executes shell commands
29 *
30 * @since 1.30
31 *
32 * Use call chaining with this class for expressiveness:
33 * $result = Shell::command( 'some command' )
34 * ->environment( [ 'ENVIRONMENT_VARIABLE' => 'VALUE' ] )
35 * ->limits( [ 'time' => 300 ] )
36 * ->execute();
37 *
38 * ... = $result->getExitCode();
39 * ... = $result->getStdout();
40 * ... = $result->getStderr();
41 */
42 class Shell {
43
44 /**
45 * Apply a default set of restrictions for improved
46 * security out of the box.
47 *
48 * Equal to NO_ROOT | SECCOMP | PRIVATE_DEV
49 *
50 * @note This value will change over time to provide increased security
51 * by default, and is not guaranteed to be backwards-compatible.
52 * @since 1.31
53 */
54 const RESTRICT_DEFAULT = 7;
55
56 /**
57 * Disallow any root access. Any setuid binaries
58 * will be run without elevated access.
59 *
60 * @since 1.31
61 */
62 const NO_ROOT = 1;
63
64 /**
65 * Use seccomp to block dangerous syscalls
66 * @see <https://en.wikipedia.org/wiki/seccomp>
67 *
68 * @since 1.31
69 */
70 const SECCOMP = 2;
71
72 /**
73 * Create a private /dev
74 *
75 * @since 1.31
76 */
77 const PRIVATE_DEV = 4;
78
79 /**
80 * Restrict the request to have no
81 * network access
82 *
83 * @since 1.31
84 */
85 const NO_NETWORK = 8;
86
87 /**
88 * Deny execve syscall with seccomp
89 * @see <https://en.wikipedia.org/wiki/exec_(system_call)>
90 *
91 * @since 1.31
92 */
93 const NO_EXECVE = 16;
94
95 /**
96 * Returns a new instance of Command class
97 *
98 * @param string|string[] $command String or array of strings representing the command to
99 * be executed, each value will be escaped.
100 * Example: [ 'convert', '-font', 'font name' ] would produce "'convert' '-font' 'font name'"
101 * @return Command
102 */
103 public static function command( $command ) {
104 $args = func_get_args();
105 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
106 // If only one argument has been passed, and that argument is an array,
107 // treat it as a list of arguments
108 $args = reset( $args );
109 }
110 $command = MediaWikiServices::getInstance()
111 ->getShellCommandFactory()
112 ->create();
113
114 return $command->params( $args );
115 }
116
117 /**
118 * Check if this class is effectively disabled via php.ini config
119 *
120 * @return bool
121 */
122 public static function isDisabled() {
123 static $disabled = null;
124
125 if ( is_null( $disabled ) ) {
126 if ( !function_exists( 'proc_open' ) ) {
127 wfDebug( "proc_open() is disabled\n" );
128 $disabled = true;
129 } else {
130 $disabled = false;
131 }
132 }
133
134 return $disabled;
135 }
136
137 /**
138 * Version of escapeshellarg() that works better on Windows.
139 *
140 * Originally, this fixed the incorrect use of single quotes on Windows
141 * (https://bugs.php.net/bug.php?id=26285) and the locale problems on Linux in
142 * PHP 5.2.6+ (bug backported to earlier distro releases of PHP).
143 *
144 * @param string $args,... strings to escape and glue together, or a single array of
145 * strings parameter
146 * @return string
147 */
148 public static function escape( /* ... */ ) {
149 $args = func_get_args();
150 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
151 // If only one argument has been passed, and that argument is an array,
152 // treat it as a list of arguments
153 $args = reset( $args );
154 }
155
156 $first = true;
157 $retVal = '';
158 foreach ( $args as $arg ) {
159 if ( !$first ) {
160 $retVal .= ' ';
161 } else {
162 $first = false;
163 }
164
165 if ( wfIsWindows() ) {
166 // Escaping for an MSVC-style command line parser and CMD.EXE
167 // Refs:
168 // * https://web.archive.org/web/20020708081031/http://mailman.lyra.org/pipermail/scite-interest/2002-March/000436.html
169 // * https://technet.microsoft.com/en-us/library/cc723564.aspx
170 // * T15518
171 // * CR r63214
172 // Double the backslashes before any double quotes. Escape the double quotes.
173 $tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE );
174 $arg = '';
175 $iteration = 0;
176 foreach ( $tokens as $token ) {
177 if ( $iteration % 2 == 1 ) {
178 // Delimiter, a double quote preceded by zero or more slashes
179 $arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
180 } elseif ( $iteration % 4 == 2 ) {
181 // ^ in $token will be outside quotes, need to be escaped
182 $arg .= str_replace( '^', '^^', $token );
183 } else { // $iteration % 4 == 0
184 // ^ in $token will appear inside double quotes, so leave as is
185 $arg .= $token;
186 }
187 $iteration++;
188 }
189 // Double the backslashes before the end of the string, because
190 // we will soon add a quote
191 $m = [];
192 if ( preg_match( '/^(.*?)(\\\\+)$/', $arg, $m ) ) {
193 $arg = $m[1] . str_replace( '\\', '\\\\', $m[2] );
194 }
195
196 // Add surrounding quotes
197 $retVal .= '"' . $arg . '"';
198 } else {
199 $retVal .= escapeshellarg( $arg );
200 }
201 }
202 return $retVal;
203 }
204 }