File: partial_revokes.h

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 (472 lines) | stat: -rw-r--r-- 17,482 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* Copyright (c) 2018, 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 */

#ifndef PARTIAL_REVOKES_INCLUDED
#define PARTIAL_REVOKES_INCLUDED

#include <map>
#include <memory>
#include <set>
#include <unordered_map>

#include "map_helpers.h"
#include "memory_debugging.h"
#include "my_inttypes.h"
#include "my_sqlcommand.h"
#include "sql/auth/auth_common.h"
#include "sql/auth/auth_utility.h"

// Forward declarations
class THD;
class ACL_USER;
class Json_array;
class Json_object;
class Restrictions_aggregator;

// Alias declarations
using db_revocations = std::unordered_map<std::string, Access_bitmask>;
using Db_access_map = std::map<std::string, Access_bitmask>;

/**
  Abstract class for ACL restrictions.
*/
class Abstract_restrictions {
 public:
  explicit Abstract_restrictions();
  virtual ~Abstract_restrictions();
  virtual bool is_empty() const = 0;
  virtual size_t size() const = 0;
  virtual void clear() = 0;
};

/**
  DB Restrictions representation in memory.

  Note that an instance of this class is owned by the security context.
  Many of the usage pattern of the security context has complex life cycle, it
  may be using memory allocated through MEM_ROOT. That may lead to an
  unwarranted memory growth in some circumstances. Therefore, we wish to own the
  life cycle of the non POD type members in this class. Please allocate them
  dynamically otherwise you may cause some difficult to find memory leaks.

  @@note : non POD members are allocated when needed but not in constructor to
  avoid unnecessary memory allocations since it is frequently accessed code
  path. Onus is on the user to call the APIs safely that is to make sure that if
  the accessed member in the API is allocated if it was supposed to be.

  DB_restrictions also provides functions to:
  - Manage DB restrictions
  - Status functions
  - Transformation of in memory db restrictions
*/
class DB_restrictions final : public Abstract_restrictions {
 public:
  DB_restrictions();
  ~DB_restrictions() override;

  db_revocations &operator()(void) { return db_restrictions(); }
  DB_restrictions(const DB_restrictions &restrictions);
  DB_restrictions(DB_restrictions &&restrictions) = delete;
  DB_restrictions &operator=(const DB_restrictions &restrictions);
  DB_restrictions &operator=(DB_restrictions &&restrictions);
  bool operator==(const DB_restrictions &restrictions) const;
  void add(const std::string &db_name, const Access_bitmask revoke_privs);
  void add(const DB_restrictions &restrictions);
  bool add(const Json_object &json_object);

  void remove(const std::string &db_name, const Access_bitmask revoke_privs);
  void remove(const Access_bitmask revoke_privs);

  bool find(const std::string &db_name, Access_bitmask &access) const;
  bool is_empty() const override;
  size_t size() const override;
  void clear() override;
  void get_as_json(Json_array &restrictions_array) const;
  const db_revocations &get() const;
  bool has_more_restrictions(const DB_restrictions &, Access_bitmask) const;

 private:
  db_revocations &db_restrictions();
  void remove(const Access_bitmask remove_restrictions,
              Access_bitmask &restrictions_mask) const noexcept;
  db_revocations *create_restrictions_if_needed();
  void copy_restrictions(const DB_restrictions &other);

 private:
  /**
    Database restrictions.
    Dynamically allocating the memory everytime in constructor would be
    expensive because this is frequently accessed code path. Therefore, we shall
    allocate the memory when needed later on.
  */
  db_revocations *m_restrictions = nullptr;
};

inline const db_revocations &DB_restrictions::get() const {
  assert(m_restrictions != nullptr);
  return *m_restrictions;
}

inline db_revocations *DB_restrictions::create_restrictions_if_needed() {
  if (!m_restrictions) {
    m_restrictions = new db_revocations();
  }
  return m_restrictions;
}

inline db_revocations &DB_restrictions::db_restrictions() {
  assert(m_restrictions != nullptr);
  return *m_restrictions;
}

inline void DB_restrictions::copy_restrictions(const DB_restrictions &other) {
  assert(m_restrictions == nullptr);
  if (other.m_restrictions) {
    m_restrictions = new db_revocations(*other.m_restrictions);
  }
}

/**
  Container of all restrictions for a given user.

  Each object created in the MEM_ROOT has to be destroyed manually.
  It will be the client's responsibility that create the objects.
*/
class Restrictions {
 public:
  explicit Restrictions();

  Restrictions(const Restrictions &) = default;
  Restrictions(Restrictions &&);
  Restrictions &operator=(const Restrictions &);
  Restrictions &operator=(Restrictions &&);
  bool has_more_db_restrictions(const Restrictions &, Access_bitmask);

  ~Restrictions();

  const DB_restrictions &db() const;
  void set_db(const DB_restrictions &db_restrictions);
  void clear_db();
  bool is_empty() const;

 private:
  /** Database restrictions */
  DB_restrictions m_db_restrictions;
};

/**
  Factory class that solely creates an object of type Restrictions_aggregator.

  - The concrete implementations of Restrictions_aggregator cannot be created
    directly since their constructors are private. This class is declared as
    friend in those concrete implementations.
  - It also records the CURRENT_USER in the binlog so that partial_revokes can
    be executed on the replica with context of current user
*/
class Restrictions_aggregator_factory {
 public:
  static std::unique_ptr<Restrictions_aggregator> create(
      THD *thd, const ACL_USER *acl_user, const char *db,
      const Access_bitmask rights, bool is_grant_revoke_all_on_db);

  static std::unique_ptr<Restrictions_aggregator> create(
      const Auth_id &grantor, const Auth_id &grantee,
      const Access_bitmask grantor_access, const Access_bitmask grantee_access,
      const DB_restrictions &grantor_restrictions,
      const DB_restrictions &grantee_restrictions,
      const Access_bitmask required_access, Db_access_map *db_map);

 private:
  static Auth_id fetch_grantor(const Security_context *sctx);
  static Auth_id fetch_grantee(const ACL_USER *acl_user);
  static Access_bitmask fetch_grantor_db_access(THD *thd, const char *db);
  static Access_bitmask fetch_grantee_db_access(THD *thd,
                                                const ACL_USER *acl_user,
                                                const char *db);
  static void fetch_grantor_access(const Security_context *sctx, const char *db,
                                   Access_bitmask &global_access,
                                   Restrictions &restrictions);
  static void fetch_grantee_access(const ACL_USER *grantee,
                                   Access_bitmask &access,
                                   Restrictions &restrictions);
};

/**
  Base class to perform aggregation of two restriction lists

  Aggregation is required if all of the following requirements are met:
  1. Partial revocation feature is enabled
  2. GRANT/REVOKE operation
  3. Either grantor or grantee or both have restrictions associated with them

  Task of the aggregator is to evaluate updates required for grantee's
  restriction. Based on restrictions associated with grantor/grantee:
  A. Add additional restrictions
     E.g. - GRANT of a new privileges by a grantor who has restrictions for
            privileges being granted
          - Creation of restrictions through REVOKE
  B. Remove some restrictions
     E.g. - GRANT of existing privileges by a grantor without restrictions
          - REVOKE of existing privileges

*/
class Restrictions_aggregator {
 public:
  virtual ~Restrictions_aggregator();

  /* interface methods which derived classes have to implement */
  virtual bool generate(Abstract_restrictions &restrictions) = 0;
  virtual bool find_if_require_next_level_operation(
      Access_bitmask &rights) const = 0;

 protected:
  Restrictions_aggregator(const Auth_id &grantor, const Auth_id grantee,
                          const Access_bitmask grantor_global_access,
                          const Access_bitmask grantee_global_access,
                          const Access_bitmask requested_access);
  Restrictions_aggregator(const Restrictions_aggregator &) = delete;
  Restrictions_aggregator &operator=(const Restrictions_aggregator &) = delete;
  Restrictions_aggregator(const Restrictions_aggregator &&) = delete;
  Restrictions_aggregator &operator=(const Restrictions_aggregator &&) = delete;

  enum class Status { Error, Warning, Validated, Aggregated, No_op };

  /** Grantor information */
  const Auth_id m_grantor;

  /** Grantee information */
  const Auth_id m_grantee;

  /** Global static privileges of grantor */
  const Access_bitmask m_grantor_global_access;

  /** Global static privileges of grantee */
  const Access_bitmask m_grantee_global_access;

  /** Privileges that are being granted or revoked */
  const Access_bitmask m_requested_access;

  /** Internal status of aggregation process */
  Status m_status;
};

/**
  Restriction aggregator for database restrictions.
  An umbrella class to cover common methods.
  This is ultimately used for privilege aggregation
  in case of GRANT/REVOKE of database level privileges.
*/
class DB_restrictions_aggregator : public Restrictions_aggregator {
 public:
  bool generate(Abstract_restrictions &restrictions) override;

 protected:
  using Status = Restrictions_aggregator::Status;
  DB_restrictions_aggregator(const Auth_id &grantor, const Auth_id grantee,
                             const Access_bitmask grantor_global_access,
                             const Access_bitmask grantee_global_access,
                             const DB_restrictions &grantor_restrictions,
                             const DB_restrictions &grantee_restrictions,
                             const Access_bitmask requested_access,
                             const Security_context *sctx);
  bool find_if_require_next_level_operation(
      Access_bitmask &rights) const override;

  /* Helper methods and members for derived classes */

  bool check_db_access_and_restrictions_collision(
      const Access_bitmask grantee_db_access,
      const Access_bitmask grantee_restrictions,
      const std::string &db_name) noexcept;
  void set_if_db_level_operation(
      const Access_bitmask requested_access,
      const Access_bitmask restrictions_mask) noexcept;
  enum class SQL_OP { SET_ROLE, GLOBAL_GRANT };
  void aggregate_restrictions(SQL_OP sql_op, const Db_access_map *m_db_map,
                              DB_restrictions &restrictions);
  Access_bitmask get_grantee_db_access(const std::string &db_name) const;
  void get_grantee_db_access(const std::string &db_name,
                             Access_bitmask &access) const;

  /** Privileges that needs to be checked further through DB grants */
  Access_bitmask m_privs_not_processed = 0;

  /** Database restrictions for grantor */
  DB_restrictions m_grantor_rl;

  /** Database restrictions for grantee */
  DB_restrictions m_grantee_rl;

  /** Security context of the current user */
  const Security_context *m_sctx;

 private:
  virtual Status validate() = 0;
  virtual void aggregate(DB_restrictions &restrictions) = 0;
};

/**
  Database restriction aggregator for SET ROLE statement.
*/
class DB_restrictions_aggregator_set_role final
    : public DB_restrictions_aggregator {
  DB_restrictions_aggregator_set_role(
      const Auth_id &grantor, const Auth_id grantee,
      const Access_bitmask grantor_global_access,
      const Access_bitmask grantee_global_access,
      const DB_restrictions &grantor_restrictions,
      const DB_restrictions &grantee_restrictions,
      const Access_bitmask requested_access, Db_access_map *db_map);

  Status validate() override;
  void aggregate(DB_restrictions &db_restrictions) override;
  friend class Restrictions_aggregator_factory;

 private:
  Db_access_map *m_db_map;
};

/**
  Restriction aggregator for GRANT statement for GLOBAL privileges.
*/
class DB_restrictions_aggregator_global_grant final
    : public DB_restrictions_aggregator {
  DB_restrictions_aggregator_global_grant(
      const Auth_id &grantor, const Auth_id grantee,
      const Access_bitmask grantor_global_access,
      const Access_bitmask grantee_global_access,
      const DB_restrictions &grantor_restrictions,
      const DB_restrictions &grantee_restrictions,
      const Access_bitmask requested_access, const Security_context *sctx);

  Status validate() override;
  void aggregate(DB_restrictions &restrictions) override;
  friend class Restrictions_aggregator_factory;
};

class DB_restrictions_aggregator_global_revoke
    : public DB_restrictions_aggregator {
 protected:
  DB_restrictions_aggregator_global_revoke(
      const Auth_id &grantor, const Auth_id grantee,
      const Access_bitmask grantor_global_access,
      const Access_bitmask grantee_global_access,
      const DB_restrictions &grantor_restrictions,
      const DB_restrictions &grantee_restrictions,
      const Access_bitmask requested_access, const Security_context *sctx);
  Status validate_if_grantee_rl_not_empty();

 private:
  Status validate() override;
  void aggregate(DB_restrictions &restrictions) override;
  friend class Restrictions_aggregator_factory;
};

/**
  Restriction aggregator for REVOKE statement over GLOBAL privileges.
*/
class DB_restrictions_aggregator_global_revoke_all final
    : public DB_restrictions_aggregator_global_revoke {
  DB_restrictions_aggregator_global_revoke_all(
      const Auth_id &grantor, const Auth_id grantee,
      const Access_bitmask grantor_global_access,
      const Access_bitmask grantee_global_access,
      const DB_restrictions &grantor_restrictions,
      const DB_restrictions &grantee_restrictions,
      const Access_bitmask requested_access, const Security_context *sctx);
  Status validate() override;
  void aggregate(DB_restrictions &restrictions) override;
  friend class Restrictions_aggregator_factory;
};

/**
  Restriction aggregator for GRANT statement over database privileges.
*/
class DB_restrictions_aggregator_db_grant final
    : public DB_restrictions_aggregator {
  DB_restrictions_aggregator_db_grant(
      const Auth_id &grantor, const Auth_id grantee,
      const Access_bitmask grantor_global_access,
      const Access_bitmask grantee_global_access,
      const Access_bitmask grantor_db_access,
      const Access_bitmask grantee_db_access,
      const DB_restrictions &grantor_restrictions,
      const DB_restrictions &grantee_restrictions,
      const Access_bitmask requested_access, bool is_grant_all,
      const std::string &db_name, const Security_context *sctx);

  void aggregate(DB_restrictions &restrictions) override;
  Status validate() override;

  /** Aggregator needs to access class members */
  friend class Restrictions_aggregator_factory;

  /** Grantor's database privileges */
  const Access_bitmask m_grantor_db_access;

  /** Grantee's database privileges */
  const Access_bitmask m_grantee_db_access;

  /** Flag for GRANT ALL ON \<db\>.* TO ... */
  const bool m_is_grant_all;

  /** Target database of GRANT */
  const std::string m_db_name;
};

/**
  Restriction aggregator for REVOKE statement for database privileges.
*/
class DB_restrictions_aggregator_db_revoke final
    : public DB_restrictions_aggregator {
  DB_restrictions_aggregator_db_revoke(
      const Auth_id &grantor, const Auth_id grantee,
      const Access_bitmask grantor_global_access,
      const Access_bitmask grantee_global_access,
      const Access_bitmask grantor_db_access,
      const Access_bitmask grantee_db_access,
      const DB_restrictions &grantor_restrictions,
      const DB_restrictions &grantee_restrictions,
      const Access_bitmask requested_access, bool is_revoke_all,
      const std::string &db_name, const Security_context *sctx);

  void aggregate(DB_restrictions &restrictions) override;
  Status validate() override;

  /** Aggregator needs to access class members */
  friend class Restrictions_aggregator_factory;

  /** Grantor's database privileges */
  const Access_bitmask m_grantor_db_access;

  /** Grantee's database privileges */
  const Access_bitmask m_grantee_db_access;

  /** Flag for GRANT ALL ON \<db\>.* TO ... */
  const bool m_is_revoke_all;

  /** Target database of REVOKE */
  const std::string m_db_name;
};

#endif /* PARTIAL_REVOKES_INCLUDED */