CVE-2022-24734: Improper Control of Generation of Code ('Code Injection') (CWE-94)
value: MyBB is a free and open source forum software. In affected versions the Admin CP's Settings management module does not validate setting types correctly on insertion and update, making it possible to add settings of supported type `php` with PHP code, executed on on _Change Settings_ pages. This results in a Remote Code Execution (RCE) vulnerability. The vulnerable module requires Admin CP access with the `Can manage settings?` permission. MyBB's Settings module, which allows administrators to add, edit, and delete non-default settings, stores setting data in an options code string ($options_code; mybb_settings.optionscode database column) that identifies the setting type and its options, separated by a new line character (\n). In MyBB 1.2.0, support for setting type php was added, for which the remaining part of the options code is PHP code executed on Change Settings pages (reserved for plugins and internal use). MyBB 1.8.30 resolves this issue. There are no known workarounds.
CWE description: The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.
CVSS v3 base score: 7.2
Published: 2022-03-09T22:15Z
Repository: https://github.com/mybb/mybb
Language: PHP
Files changed: admin/modules/config/settings.php
Commit date: 2022-03-09 18:46:34 +0100
Commit message: Fix ACP Settings management RCE

Diff: diff --git a/admin/modules/config/settings.php b/admin/modules/config/settings.php
index 912fb4662..b4ffc9149 100644
--- a/admin/modules/config/settings.php
+++ b/admin/modules/config/settings.php
@@ -291,12 +291,12 @@ if($mybb->input['action'] == "add")
 			$dup_setting_title = $db->fetch_field($query, 'title');
 			$errors[] = $lang->sprintf($lang->error_duplicate_name, $dup_setting_title);
 		}
 
 		// do some type filtering
-		$mybb->input['type'] = str_replace("\n", "", $mybb->input['type']);
-		if(strtolower(substr($mybb->input['type'], 0, 3)) == "php")
+		$mybb->input['type'] = $mybb->get_input('type');
+		if(!ctype_alnum($mybb->input['type']) || strtolower($mybb->input['type']) == "php")
 		{
 			$mybb->input['type'] = "";
 		}
 
 		if(!$mybb->input['type'])
@@ -494,12 +494,12 @@ if($mybb->input['action'] == "edit")
 			$dup_setting_title = $db->fetch_field($query, 'title');
 			$errors[] = $lang->sprintf($lang->error_duplicate_name, $dup_setting_title);
 		}
 
 		// do some type filtering
-		$mybb->input['type'] = str_replace("\n", "", $mybb->input['type']);
-		if(strtolower(substr($mybb->input['type'], 0, 3)) == "php")
+		$mybb->input['type'] = $mybb->get_input('type');
+		if(!ctype_alnum($mybb->input['type']) || strtolower($mybb->input['type']) == "php")
 		{
 			$mybb->input['type'] = "";
 		}
 
 		if(!$mybb->input['type'])

Vulnerable code: 		$mybb->input['type'] = str_replace("\n", "", $mybb->input['type']);
		if(strtolower(substr($mybb->input['type'], 0, 3)) == "php")
		$mybb->input['type'] = str_replace("\n", "", $mybb->input['type']);
		if(strtolower(substr($mybb->input['type'], 0, 3)) == "php")
Fixed code: 		$mybb->input['type'] = $mybb->get_input('type');
		if(!ctype_alnum($mybb->input['type']) || strtolower($mybb->input['type']) == "php")
		$mybb->input['type'] = $mybb->get_input('type');
		if(!ctype_alnum($mybb->input['type']) || strtolower($mybb->input['type']) == "php")