File: dynamic_privileges_impl.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (259 lines) | stat: -rw-r--r-- 10,043 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* Copyright (c) 2017, 2025, Oracle and/or its affiliates.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.

This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation.  The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License, version 2.0, for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
#include "sql/auth/dynamic_privileges_impl.h"

#include <ctype.h>
#include <mysql/components/my_service.h>
#include <mysql/components/service_implementation.h>
#include <mysql/components/services/dynamic_privilege.h>
#include <mysql/service_plugin_registry.h>
#include <stddef.h>
#include <string>
#include <unordered_set>
#include <utility>

#include "m_string.h"
#include "mysql/components/service.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/components/services/registry.h"
#include "sql/auth/dynamic_privilege_table.h"
#include "sql/auth/sql_auth_cache.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/current_thd.h"
#include "sql/mysqld_thd_manager.h"
#include "sql/sql_thd_internal_api.h"  // create_internal_thd

class THD;

/**
  This helper class is used for either selecting a previous THD or
  if it's missing, create a new THD.
*/
class Thd_creator {
 public:
  Thd_creator(THD *thd) : m_thd(thd), m_tmp_thd(nullptr) {}

  /**
    Returns a THD handle either by creating a new one or by returning a
    previously created THD.
  */
  THD *operator()() {
    if (m_thd == nullptr && m_tmp_thd == nullptr) {
      /*
        Initiate a THD without plugins,
        without attaching to the Global_THD_manager, and without setting
        an OS thread ID.
        The global THD manager is still needed to create the thread through.
      */
      assert(Global_THD_manager::is_initialized());
      m_tmp_thd = create_internal_thd();
      return m_tmp_thd;
    } else if (m_thd == nullptr) {
      return m_tmp_thd;
    }
    return m_thd;
  }

  /**
    Automatically frees any THD handle created by this class.
  */
  ~Thd_creator() {
    if (m_thd == nullptr && m_tmp_thd != nullptr) {
      destroy_internal_thd(m_tmp_thd);
    }
  }

 private:
  THD *m_thd;
  THD *m_tmp_thd;
};

/**
  Register a privilege identifiers in the list of known identifiers. This
  enable the SQL syntax to recognize the identifier as a valid token.
  @param privilege_str The privilege identifier string
  @param privilege_str_len The length of the identifier string

  @note This function acquires the THD from the current_thd

  @returns Error flag
    @return true The privilege ID couldn't be inserted.
    @return false The privilege ID was successfully registered.
*/

DEFINE_BOOL_METHOD(dynamic_privilege_services_impl::register_privilege,
                   (const char *privilege_str, size_t privilege_str_len)) {
  try {
    std::string priv;
    const char *c = &privilege_str[0];
    for (size_t i = 0; i < privilege_str_len; ++i, ++c)
      priv.append(1, static_cast<char>(toupper(*c)));

    Thd_creator get_thd(current_thd);
    Acl_cache_lock_guard acl_cache_lock(get_thd(),
                                        Acl_cache_lock_mode::WRITE_MODE);
    acl_cache_lock.lock();
    Dynamic_privilege_register *reg = get_dynamic_privilege_register();
    if (reg->find(priv) != reg->end()) {
      /* If the privilege ID already is registered; report success */
      return false;
    }
    return !get_dynamic_privilege_register()->insert(priv).second;
  } catch (...) {
    return true;
  }
}

/**
  Unregister a privilege identifiers in the list of known identifiers. This
  disables the SQL syntax from recognizing the identifier as a valid token.
  @param privilege_str The privilege identifier string
  @param privilege_str_len The length of the identifier string

  @note This function acquires the THD from the current_thd

  @returns Error flag
    @return true The privilege ID wasn't in the list or remove failed.
    @return false The privilege ID was successfully unregistered.
*/

DEFINE_BOOL_METHOD(dynamic_privilege_services_impl::unregister_privilege,
                   (const char *privilege_str, size_t privilege_str_len)) {
  try {
    std::string priv;
    const char *c = &privilege_str[0];
    for (size_t i = 0; i < privilege_str_len; ++i, ++c)
      priv.append(1, static_cast<char>(toupper(*c)));

    /*
      This function may be called after the thd manager is gone, e.g.
      from component deinitialization.
      In this case it can just remove the priv from the global list
      without taking locks.
    */
    if (Global_THD_manager::is_initialized()) {
      Thd_creator get_thd(current_thd);
      Acl_cache_lock_guard acl_cache_lock(get_thd(),
                                          Acl_cache_lock_mode::WRITE_MODE);
      DBUG_EXECUTE_IF("bug34594035_simulate_lock_failure",
                      DBUG_SET("+d,bug34594035_fail_acl_cache_lock"););
      acl_cache_lock.lock();
      return (get_dynamic_privilege_register()->erase(priv) == 0);
    } else
      return (get_dynamic_privilege_register()->erase(priv) == 0);
  } catch (...) {
    return true;
  }
}

/**
  Checks if a user has a specified privilege ID granted to it.

  @param handle The active security context of the user to be checked.
  @param privilege_str The privilege identifier string
  @param privilege_str_len The length of the identifier string

  @returns Success state
    @return true The user has the grant
    @return false The user hasn't the grant
*/

DEFINE_BOOL_METHOD(dynamic_privilege_services_impl::has_global_grant,
                   (Security_context_handle handle, const char *privilege_str,
                    size_t privilege_str_len)) {
  Security_context *sctx = reinterpret_cast<Security_context *>(handle);
  return sctx->has_global_grant(privilege_str, privilege_str_len).first;
}

/**
  Bootstrap the dynamic privilege service by seeding it with server
  implementation-specific data.
*/

bool dynamic_privilege_init(void) {
  // Set up default dynamic privileges
  SERVICE_TYPE(registry) *r = mysql_plugin_registry_acquire();
  int ret = false;
  {
    my_service<SERVICE_TYPE(dynamic_privilege_register)> service(
        "dynamic_privilege_register.mysql_server", r);
    if (service.is_valid()) {
      ret += service->register_privilege(STRING_WITH_LEN("ROLE_ADMIN"));
      ret += service->register_privilege(
          STRING_WITH_LEN("SYSTEM_VARIABLES_ADMIN"));
      ret += service->register_privilege(STRING_WITH_LEN("BINLOG_ADMIN"));
      ret += service->register_privilege(
          STRING_WITH_LEN("REPLICATION_SLAVE_ADMIN"));
      ret += service->register_privilege(
          STRING_WITH_LEN("GROUP_REPLICATION_ADMIN"));
      ret +=
          service->register_privilege(STRING_WITH_LEN("ENCRYPTION_KEY_ADMIN"));
      ret += service->register_privilege(STRING_WITH_LEN("CONNECTION_ADMIN"));
      ret += service->register_privilege(STRING_WITH_LEN("SET_USER_ID"));
      ret += service->register_privilege(STRING_WITH_LEN("XA_RECOVER_ADMIN"));
      ret += service->register_privilege(
          STRING_WITH_LEN("PERSIST_RO_VARIABLES_ADMIN"));
      ret += service->register_privilege(STRING_WITH_LEN("BACKUP_ADMIN"));
      ret += service->register_privilege(STRING_WITH_LEN("CLONE_ADMIN"));
      ret +=
          service->register_privilege(STRING_WITH_LEN("RESOURCE_GROUP_ADMIN"));
      ret +=
          service->register_privilege(STRING_WITH_LEN("RESOURCE_GROUP_USER"));
      ret += service->register_privilege(
          STRING_WITH_LEN("SESSION_VARIABLES_ADMIN"));
      ret += service->register_privilege(
          STRING_WITH_LEN("BINLOG_ENCRYPTION_ADMIN"));
      ret += service->register_privilege(
          STRING_WITH_LEN("SERVICE_CONNECTION_ADMIN"));
      ret += service->register_privilege(
          STRING_WITH_LEN("APPLICATION_PASSWORD_ADMIN"));
      ret += service->register_privilege(STRING_WITH_LEN("SYSTEM_USER"));
      ret += service->register_privilege(
          STRING_WITH_LEN("TABLE_ENCRYPTION_ADMIN"));
      ret += service->register_privilege(STRING_WITH_LEN("AUDIT_ADMIN"));
      ret +=
          service->register_privilege(STRING_WITH_LEN("TELEMETRY_LOG_ADMIN"));
      ret +=
          service->register_privilege(STRING_WITH_LEN("REPLICATION_APPLIER"));
      ret += service->register_privilege(STRING_WITH_LEN("SHOW_ROUTINE"));
      ret += service->register_privilege(
          STRING_WITH_LEN("INNODB_REDO_LOG_ENABLE"));
      ret +=
          service->register_privilege(STRING_WITH_LEN("FLUSH_OPTIMIZER_COSTS"));
      ret += service->register_privilege(STRING_WITH_LEN("FLUSH_STATUS"));
      ret +=
          service->register_privilege(STRING_WITH_LEN("FLUSH_USER_RESOURCES"));
      ret += service->register_privilege(STRING_WITH_LEN("FLUSH_TABLES"));
      ret += service->register_privilege(
          STRING_WITH_LEN("GROUP_REPLICATION_STREAM"));
      ret += service->register_privilege(
          STRING_WITH_LEN("AUTHENTICATION_POLICY_ADMIN"));
      ret += service->register_privilege(
          STRING_WITH_LEN("PASSWORDLESS_USER_ADMIN"));
      ret += service->register_privilege(
          STRING_WITH_LEN("SENSITIVE_VARIABLES_OBSERVER"));
    }
  }  // exist scope
  mysql_plugin_registry_release(r);
  return ret != 0;
}