Bug #121790 debug crash while set variable using prepare stmt

Submitted: 16 Nov 8:41
Reporter: ximin liang (OCA) Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Prepared statements Severity:S3 (Non-critical)
Version:26.7.10 OS:Any
Assigned to: CPU Architecture:Any

[16 Nov 8:41] ximin liang
Description:
Hello MySQL team:

  here is a debug assert fail while set variable using prepare stmt.

How to repeat:
CREATE USER 'test'@'%' IDENTIFIED BY 'test';  
-- log using test
PREPARE s1 FROM 'SET GLOBAL max_connections=4321';
--crash

Suggested fix:
diff --git a/mysql-test/suite/auth_sec/r/prepare_set_global_privilege.result b/mysql-test/suite/auth_sec/r/prepare_set_global_privilege.result
new file mode 100644
index 00000000000..bb072d198e5
--- /dev/null
+++ b/mysql-test/suite/auth_sec/r/prepare_set_global_privilege.result
@@ -0,0 +1,10 @@
+CREATE USER u1@localhost;
+PREPARE stmt FROM 'SET GLOBAL max_connections=1000';
+ERROR 42000: Access denied; you need (at least one of) the SUPER or SYSTEM_VARIABLES_ADMIN privilege(s) for this operation
+PREPARE stmt FROM 'SET PERSIST max_connections=1000';
+ERROR 42000: Access denied; you need (at least one of) the SUPER or SYSTEM_VARIABLES_ADMIN privilege(s) for this operation
+PREPARE stmt FROM 'SET PERSIST_ONLY max_connections=1000';
+ERROR 42000: Access denied; you need SYSTEM_VARIABLES_ADMIN and PERSIST_RO_VARIABLES_ADMIN privileges for this operation
+PREPARE stmt FROM 'SET SESSION sort_buffer_size=1000000';
+DEALLOCATE PREPARE stmt;
+DROP USER u1@localhost;
diff --git a/mysql-test/suite/auth_sec/t/prepare_set_global_privilege.test b/mysql-test/suite/auth_sec/t/prepare_set_global_privilege.test
new file mode 100644
index 00000000000..2b117047e0d
--- /dev/null
+++ b/mysql-test/suite/auth_sec/t/prepare_set_global_privilege.test
@@ -0,0 +1,26 @@
+# Bug#XXXXX: PREPARE of a SET GLOBAL/PERSIST statement by a user without
+# SYSTEM_VARIABLES_ADMIN must report the privileged access error.
+#
+# Before the fix set_var::light_check() returned an error without raising it,
+# leaving the diagnostics area empty, which tripped the DA_EMPTY assertion in
+# THD::send_statement_status() and killed a debug server.
+
+CREATE USER u1@localhost;
+
+--connect(con1,localhost,u1)
+
+--error ER_SPECIFIC_ACCESS_DENIED_ERROR
+PREPARE stmt FROM 'SET GLOBAL max_connections=1000';
+--error ER_SPECIFIC_ACCESS_DENIED_ERROR
+PREPARE stmt FROM 'SET PERSIST max_connections=1000';
+--error ER_PERSIST_ONLY_ACCESS_DENIED_ERROR
+PREPARE stmt FROM 'SET PERSIST_ONLY max_connections=1000';
+
+# A statement which needs no privilege must still prepare fine.
+PREPARE stmt FROM 'SET SESSION sort_buffer_size=1000000';
+DEALLOCATE PREPARE stmt;
+
+--disconnect con1
+
+--connection default
+DROP USER u1@localhost;
diff --git a/sql/set_var.cc b/sql/set_var.cc
index d7d38d6b46e..0e28b819b9c 100644
--- a/sql/set_var.cc
+++ b/sql/set_var.cc
@@ -1811,19 +1811,17 @@ int set_var::light_check(THD *thd) {
   if (m_var_tracker.access_system_variable<bool>(thd, f).value_or(true)) {
     return -1;
   }
-  Security_context *sctx = thd->security_context();
-  if ((type == OPT_GLOBAL || type == OPT_PERSIST) &&
-      !(sctx->check_access(SUPER_ACL) ||
-        sctx->has_global_grant(STRING_WITH_LEN("SYSTEM_VARIABLES_ADMIN"))
-            .first))
+  /*
+    Reuse the privilege check of set_var::resolve() so that the very same
+    error is reported in the prepared statement case. Reporting the error is
+    mandatory: the caller treats a non-zero return as "error message already
+    sent", and an empty diagnostics area trips the DA_EMPTY assertion in
+    THD::send_statement_status().
+  */
+  if ((type == OPT_GLOBAL || type == OPT_PERSIST) && check_priv(thd, false))
     return 1;
 
-  if ((type == OPT_PERSIST_ONLY) &&
-      !(sctx->has_global_grant(STRING_WITH_LEN("PERSIST_RO_VARIABLES_ADMIN"))
-            .first &&
-        sctx->has_global_grant(STRING_WITH_LEN("SYSTEM_VARIABLES_ADMIN"))
-            .first))
-    return 1;
+  if (type == OPT_PERSIST_ONLY && check_priv(thd, true)) return 1;
 
   if (value == nullptr || value->fixed) return 0;

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.048 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.