From c499b15fe04574b40eddebfb17e006d0db482463 Mon Sep 17 00:00:00 2001 From: Matteo Scaramuccia Date: Thu, 8 Mar 2018 23:27:25 +0100 Subject: [PATCH] MDL-61626 core_dml: pgsql and schema qualification. --- lib/dml/pgsql_native_moodle_database.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/dml/pgsql_native_moodle_database.php b/lib/dml/pgsql_native_moodle_database.php index 44d298c03a6..a85023dc054 100644 --- a/lib/dml/pgsql_native_moodle_database.php +++ b/lib/dml/pgsql_native_moodle_database.php @@ -345,13 +345,15 @@ class pgsql_native_moodle_database extends moodle_database { if ($result) { while ($row = pg_fetch_assoc($result)) { - if (!preg_match('/CREATE (|UNIQUE )INDEX ([^\s]+) ON '.$tablename.' USING ([^\s]+) \(([^\)]+)\)/i', $row['indexdef'], $matches)) { + // The index definition could be generated schema-qualifying the target table name + // for safety, depending on the pgsql version (CVE-2018-1058). + if (!preg_match('/CREATE (|UNIQUE )INDEX ([^\s]+) ON (|'.$row['schemaname'].'\.)'.$tablename.' USING ([^\s]+) \(([^\)]+)\)/i', $row['indexdef'], $matches)) { continue; } - if ($matches[4] === 'id') { + if ($matches[5] === 'id') { continue; } - $columns = explode(',', $matches[4]); + $columns = explode(',', $matches[5]); foreach ($columns as $k=>$column) { $column = trim($column); if ($pos = strpos($column, ' ')) {