ryp 1 месяц назад
Родитель
Сommit
7983002b30

+ 25 - 2
judger/uoj_judger/include/uoj_judger.h

@@ -346,12 +346,35 @@ int conf_int(const string &key, int num, const int &val) {
 int conf_int(const string &key)  {
 	return conf_int(key, 0);
 }
+// problem.conf is uploaded by whoever controls the problem, so input_pre /
+// input_suf / output_pre / output_suf are attacker-controlled.  They get
+// concatenated into data_path / work_path by conf_input_file_name() and
+// conf_output_file_name(), so a value like "x/../../../../etc/" used to make
+// the judger read and write outside the problem's own directory.  The language
+// side of that bug was remote code execution (the web root was writable); here
+// it is "only" arbitrary read/write inside the judge container, but the same
+// whitelist applies: a filename component is all that is ever legitimate.
+string conf_filename_part(const string &key, const string &val) {
+	string s = conf_str(key, val);
+	if (s.find("..") != string::npos) {
+		return val;
+	}
+	for (size_t i = 0; i < s.size(); ++i) {
+		char c = s[i];
+		bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
+		          (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '-';
+		if (!ok) {
+			return val;
+		}
+	}
+	return s;
+}
 string conf_input_file_name(int num) {
 	ostringstream name;
 	if (num < 0) {
 		name << "ex_";
 	}
-	name << conf_str("input_pre", "input") << abs(num) << "." << conf_str("input_suf", "txt");
+	name << conf_filename_part("input_pre", "input") << abs(num) << "." << conf_filename_part("input_suf", "txt");
 	return name.str();
 }
 string conf_output_file_name(int num) {
@@ -359,7 +382,7 @@ string conf_output_file_name(int num) {
 	if (num < 0) {
 		name << "ex_";
 	}
-	name << conf_str("output_pre", "output") << abs(num) << "." << conf_str("output_suf", "txt");
+	name << conf_filename_part("output_pre", "output") << abs(num) << "." << conf_filename_part("output_suf", "txt");
 	return name.str();
 }
 RunLimit conf_run_limit(string pre, const int &num, const RunLimit &val) {

+ 17 - 2
web/app/libs/uoj-data-lib.php

@@ -354,12 +354,12 @@
 		return (new SyncProblemDataHandler($problem, $user))->handle();
 	}
 	function dataAddExtraTest($problem, $input_file_name, $output_file_name) {
-		$id = $problem['id'];
+		$id = (int)$problem['id'];
 
 		$cur_dir = "/var/uoj_data/upload/$id";
 
 		# BY ryp!!!!, 20250827
-		exec ("ln -sf /var/uoj_data/$id /var/uoj_data/upload/$id");
+		exec("ln -sf /var/uoj_data/$id /var/uoj_data/upload/$id");
 		
 		$problem_conf = getUOJConf("{$cur_dir}/problem.conf");
 		if ($problem_conf == -1 || $problem_conf == -2) {
@@ -369,6 +369,21 @@
 		
 		$new_input_name = getUOJProblemExtraInputFileName($problem_conf, $problem_conf['n_ex_tests']);
 		$new_output_name = getUOJProblemExtraOutputFileName($problem_conf, $problem_conf['n_ex_tests']);
+
+		// This is the sink that turned a hostile problem.conf into remote code
+		// execution: the names come from the conf, the file contents come from a
+		// hack input, and moving the one to the other wrote a .php file into the
+		// document root.  The filename helpers now reject separators and "..",
+		// so this can only fail if $cur_dir itself hides a symlink (an uploaded
+		// zip can contain one).  Resolve both sides and refuse to write anywhere
+		// but the problem's own data directory.
+		$real_dir = realpath($cur_dir);
+		foreach (array($new_input_name, $new_output_name) as $new_name) {
+			$real_dest_dir = realpath(dirname("$cur_dir/$new_name"));
+			if ($real_dir === false || $real_dest_dir === false || $real_dest_dir !== $real_dir) {
+				return '测试数据文件名不合法,已拒绝写入';
+			}
+		}
 		
 		putUOJConf("$cur_dir/problem.conf", $problem_conf);
 		move_uploaded_file($input_file_name, "$cur_dir/$new_input_name");

+ 30 - 4
web/app/libs/uoj-judger-lib.php

@@ -145,17 +145,43 @@
 		}
 	}
 	
+	// problem.conf can also arrive inside a zip that the problem's controller
+	// uploads, so *every* value in it is attacker-controlled: the field checks in
+	// problem_data_manage.php are not a security boundary, because the zip upload
+	// path never goes through them.  input_pre / input_suf / output_pre /
+	// output_suf used to be concatenated straight into a filesystem path, so
+	//
+	//     input_pre x/../../../../../opt/uoj/web/shell.
+	//     input_suf php
+	//
+	// made dataAddExtraTest() move an uploaded hack input to
+	// /opt/uoj/web/shell.1.php -- an arbitrary-content arbitrary-path write into
+	// the document root, i.e. remote code execution as the web user.
+	//
+	// Only a bare filename component is ever legitimate here.  Anything else
+	// (separators, "..", absolute paths, ...) falls back to the default instead of
+	// escaping the problem's own data directory.  Same idea as the whitelist that
+	// already guards use_builtin_checker in uoj-data-lib.php.
+	function getUOJConfFileNamePart($conf, $key, $default_val) {
+		$val = getUOJConfVal($conf, $key, $default_val);
+		if (!is_string($val) || !preg_match('/^[A-Za-z0-9_.-]*$/', $val) || strpos($val, '..') !== false) {
+			error_log("uoj: ignoring unsafe problem.conf value for '$key'");
+			return $default_val;
+		}
+		return $val;
+	}
+
 	function getUOJProblemInputFileName($problem_conf, $num) {
-		return getUOJConfVal($problem_conf, 'input_pre', 'input') . $num . '.' . getUOJConfVal($problem_conf, 'input_suf', 'txt');
+		return getUOJConfFileNamePart($problem_conf, 'input_pre', 'input') . $num . '.' . getUOJConfFileNamePart($problem_conf, 'input_suf', 'txt');
 	}
 	function getUOJProblemOutputFileName($problem_conf, $num) {
-		return getUOJConfVal($problem_conf, 'output_pre', 'output') . $num . '.' . getUOJConfVal($problem_conf, 'output_suf', 'txt');
+		return getUOJConfFileNamePart($problem_conf, 'output_pre', 'output') . $num . '.' . getUOJConfFileNamePart($problem_conf, 'output_suf', 'txt');
 	}
 	function getUOJProblemExtraInputFileName($problem_conf, $num) {
-		return 'ex_' . getUOJConfVal($problem_conf, 'input_pre', 'input') . $num . '.' . getUOJConfVal($problem_conf, 'input_suf', 'txt');
+		return 'ex_' . getUOJConfFileNamePart($problem_conf, 'input_pre', 'input') . $num . '.' . getUOJConfFileNamePart($problem_conf, 'input_suf', 'txt');
 	}
 	function getUOJProblemExtraOutputFileName($problem_conf, $num) {
-		return 'ex_' . getUOJConfVal($problem_conf, 'output_pre', 'output') . $num . '.' . getUOJConfVal($problem_conf, 'output_suf', 'txt');
+		return 'ex_' . getUOJConfFileNamePart($problem_conf, 'output_pre', 'output') . $num . '.' . getUOJConfFileNamePart($problem_conf, 'output_suf', 'txt');
 	}
 	
 	function rejudgeProblem($problem) {