From 825fd970bd0001ec83bfb0681f163054db4b98f1 Mon Sep 17 00:00:00 2001
From: Darian Anthony Patrick <dpatrick@wikimedia.org>
Date: Tue, 19 Apr 2016 10:53:39 -0700
Subject: [PATCH] Enforce upper limit on invocations of wfShellExec()

Enforce an upper limit of 100,000 bytes on commands executed via
wfShellExec() to avoid HHVM crash resulting from process spawned with
argument exceeding MAX_ARG_STRLEN, as defined in binfmts.h

Bug: T129506
---
 includes/GlobalFunctions.php | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php
index 5c42bc2..2c7f44d 100644
--- a/includes/GlobalFunctions.php
+++ b/includes/GlobalFunctions.php
@@ -31,6 +31,8 @@ use MediaWiki\Session\SessionManager;
 // Hide compatibility functions from Doxygen
 /// @cond
 
+define( 'MW_MAX_SHELL_ARG_STRLEN', '100000');
+
 /**
  * Compatibility functions
  *
@@ -2425,6 +2427,14 @@ function wfShellExec( $cmd, &$retval = null, $environ = [],
 	}
 	wfDebug( "wfShellExec: $cmd\n" );
 
+	// Don't try to execute commands that exceed Linux's MAX_ARG_STRLEN.
+	// Other platforms may be more accomodating, but we don't want to be
+	// accomodating, because very long commands probably include user
+	// input. See T129506.
+	if ( strlen( $cmd ) > MW_MAX_SHELL_ARG_STRLEN ) {
+		throw new Exception( __METHOD__ . '(): total length of $cmd must not exceed MW_MAX_SHELL_ARG_STRLEN' );
+	}
+
 	$desc = [
 		0 => [ 'file', 'php://stdin', 'r' ],
 		1 => [ 'pipe', 'w' ],
-- 
2.5.4 (Apple Git-61)

