Bug #121923 System variable update service cannot set a SENSITIVE variable when given no THD

Submitted: 25 Nov 11:11 Modified: 26 Nov 7:00
Reporter: Tomas Ulin Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Components / Services Severity:S3 (Non-critical)
Version: 8.4.x, trunk (bf5a74e4842) OS:Any
Assigned to: CPU Architecture:Any

[25 Nov 11:11] Tomas Ulin
Description:
A component that registers a system variable with PLUGIN_VAR_SENSITIVE
cannot change that variable through the mysql_system_variable_update_string
service when it passes a NULL THD. The call fails with:

  ERROR 1227 (42000): Access denied; you need (at least one of) the
  SENSITIVE_VARIABLES_OBSERVER privilege(s) for this operation

What happens:

1. The service accepts a NULL THD for callers that have no session, such as
   a component's own background thread. In that case it creates a temporary
   THD through Storing_auto_THD.

2. create_internal_thd_ctx() in sql/server_component/storing_auto_thd.cc
   gives that THD a fixed list of dynamic privileges: ENCRYPTION_KEY_ADMIN,
   ROLE_ADMIN, SYSTEM_VARIABLES_ADMIN, AUDIT_ADMIN, TELEMETRY_LOG_ADMIN and
   PERSIST_RO_VARIABLES_ADMIN. The comment there says the THD has "minimal
   privileges needed to set system variables". SENSITIVE_VARIABLES_OBSERVER
   is not in the list.

3. Set_variables_helper::add_variable() looks the variable up with
   access_system_variable() on the temporary THD. For a SENSITIVE variable
   the lookup calls sys_var::check_if_sensitive_in_context(), which requires
   SENSITIVE_VARIABLES_OBSERVER and raises the error above.

The check is made against the temporary THD, not the session that called the
component. So the call fails even when that session holds
SENSITIVE_VARIABLES_OBSERVER, for example as root.

Passing the session's THD instead of NULL works when the session holds
SENSITIVE_VARIABLES_OBSERVER. But then the result depends on who is
connected. When a component sets its variable on behalf of a user without
that privilege, the same check runs against the user's THD and fails. A
component with no session has no THD to pass. So a component has no call
that reliably updates its own SENSITIVE variable. That rules out, for
example, a component that refreshes a credential it keeps in a SENSITIVE
variable.

The same call with a NULL THD succeeds for a variable that is not
SENSITIVE. The server's own internal write of SENSITIVE variables, when it
applies persisted values at startup or at INSTALL COMPONENT, does not hit
this: Persisted_variables_cache looks the variable up with
Force_sensitive_system_variable_access::YES.

How to repeat:
Uses the test components component_test_sensitive_system_variables and
component_test_mysql_system_variable_set. The first argument of
test_set_system_variable_string() is 0 to pass the current THD to the service
and 1 to pass NULL.

INSTALL COMPONENT 'file://component_test_sensitive_system_variables';
INSTALL COMPONENT 'file://component_test_mysql_system_variable_set';

# Works: SQL, as root
SET GLOBAL test_component.sensitive_string_1 = 'set-by-sql';

# Works: service, current THD
SELECT test_set_system_variable_string(0, 'test_component',
  'sensitive_string_1', 'set-with-thd', 'GLOBAL');

# Fails: service, THD of a session without SENSITIVE_VARIABLES_OBSERVER.
# The user holds SYSTEM_VARIABLES_ADMIN, so that is the only privilege missing.
CREATE USER u_no_observer@localhost;
GRANT SYSTEM_VARIABLES_ADMIN ON *.* TO u_no_observer@localhost;
# connected as u_no_observer:
SELECT test_set_system_variable_string(0, 'test_component',
  'sensitive_string_1', 'set-by-no-observer', 'GLOBAL');
ERROR 1227 (42000): Access denied; you need (at least one of) the
SENSITIVE_VARIABLES_OBSERVER privilege(s) for this operation

# Works: service, no THD, variable not SENSITIVE
SELECT test_set_system_variable_string(1, NULL, 'lc_messages', 'fr_FR',
  'GLOBAL');

# Fails: service, no THD, SENSITIVE variable
SELECT test_set_system_variable_string(1, 'test_component',
  'sensitive_string_1', 'set-without-thd', 'GLOBAL');

ERROR 1227 (42000): Access denied; you need (at least one of) the
SENSITIVE_VARIABLES_OBSERVER privilege(s) for this operation

Expected: the last call sets the variable, as it does for a variable that is
not SENSITIVE. The call with the THD of the session without
SENSITIVE_VARIABLES_OBSERVER is shown to illustrate that passing a THD is no
reliable alternative.

The same steps as an MTR test are attached
(mysql-test/t/sensitive_variables_update_service.test).
[25 Nov 11:12] Tomas Ulin
test

Attachment: sensitive_variables_update_service.test (application/octet-stream, text), 2.26 KiB.

[26 Nov 6:46] Tomas Ulin
test

Attachment: sys_var_read_sensitive_no_thd.test (application/octet-stream, text), 1.06 KiB.

[26 Nov 6:59] Tomas Ulin
Two related problems with system variables and no THD, filed separately:
- Bug #121942: reading a SENSITIVE variable through mysql_system_variable_reader with no THD crashes the server in System_variable_tracker::cache_metadata.
- Bug #121943: the update service with a NULL THD uses Storing_auto_THD, which calls my_thread_end() on a thread that has mysys state but no THD, such as a component thread attached through mysql_my_thread.
[26 Nov 7:00] Tomas Ulin
ignore  sys_var_read_sensitive_no_thd.test file, it was attached to the wrong report

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

Page generated in 0.041 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.