Security

Privileges: GRANT, REVOKE and SHOW GRANTS

The privilege levels and the privileges Raft DB knows at each, GRANT, REVOKE and SHOW GRANTS as implemented, how statements are checked and which errors come back.

Privileges are checked by raftdb-mysql from the parsed form of the statements listed below before they run (acl.rs); the section "Not checked yet" lists what is not. The model, the error numbers and the texts follow MySQL, and a comparison with a real MySQL 9.7 (11 blocks run as 7 different users) shows that errors, their texts, SHOW GRANTS and what each user sees are identical, except that MySQL also lists performance_schema.

Syntax#

SQL
GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ...
    ON { *.* | db_name.* | db_name.tbl_name | tbl_name }
    TO user [IDENTIFIED BY 'password'] [, user ...]
    [WITH GRANT OPTION];

REVOKE priv_type [(column_list)] [, ...]
    ON { *.* | db_name.* | db_name.tbl_name | tbl_name }
    FROM user [, user ...];

REVOKE ALL PRIVILEGES, GRANT OPTION FROM user [, user ...];

SHOW GRANTS [FOR user];

A database name in GRANT may use % as a pattern. GRANT to an account that does not exist is an error (1133); there is no implicit creation.

Levels and privileges#

LevelPrivileges that can be granted
Global (*.*)SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE (28)
Database (db.*)SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER (18)
Table (db.t)SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE VIEW, SHOW VIEW, TRIGGER (12)
ColumnSELECT, INSERT, UPDATE, REFERENCES

ALL [PRIVILEGES], USAGE and GRANT OPTION are accepted. Not all privileges are used by a statement yet: those that are only about the server (FILE, PROCESS, SHUTDOWN...) can be granted but give no use of a database. EVENT (events), TRIGGER (triggers on a table) and CREATE ROUTINE / ALTER ROUTINE (stored routines of a database) are checked. Dynamic privileges are not known (SYSTEM_VARIABLES_ADMIN and the others): GRANT of an unknown privilege is an error. Privileges on routines and roles are refused (1235).

How a statement is checked#

The needed privilege, database, table and columns are worked out from the parsed statement:

  • SELECT is needed also for the columns named in WHERE, ORDER BY and the expressions of an UPDATE.
  • REPLACE needs INSERT and DELETE; TRUNCATE needs DROP.
  • SET GLOBAL needs SUPER for a stored account (a bootstrap administrator may do it).
  • A subquery is checked like the SELECT it is, in every statement that can hold one: SET, INSERT, UPDATE, DELETE, CREATE TABLE ... AS SELECT and CREATE VIEW need SELECT on the tables the subquery or the query reads.
  • An event needs EVENT on its database to be created, changed or dropped. Naming another account as its DEFINER needs SUPER. The body runs later with the privileges of its definer, never as an administrator; an event whose definer no longer exists is skipped.
  • GRANT role TO account and SET DEFAULT ROLE ... TO another account need CREATE USER (or SUPER).
  • CREATE TRIGGER needs TRIGGER on the table; CREATE PROCEDURE / FUNCTION needs CREATE ROUTINE and DROP of one needs ALTER ROUTINE on the database.
  • SHOW DATABASES, SHOW TABLES, information_schema and DESCRIBE show only what the account may see. An account with the global SHOW DATABASES sees all databases.
  • A change to grants takes effect at the next statement of open connections (same front-end), at the next login for another front-end.

Not checked yet#

Be aware of these when you give an account limited rights on a database that other, more trusted accounts also use: they are known gaps, not decisions.

  • Sequences, CALL (the EXECUTE privilege), HANDLER and the SHOW CREATE forms for routines, triggers and events are not checked against the account's privileges.
  • A view is read with the privileges of the account that reads it, not of its definer; RENAME TABLE across databases checks only the source database.
  • The checks of the SQL Server and PostgreSQL front-ends are those of their own chapters.

Errors#

ErrorMeaning
1044The user is not allowed to use a database
1142Command denied for a table
1143Command denied for a column
1227Access denied; you need a global privilege (for example SUPER)
1045Access denied at login
1141No such grant defined for the user on the host (SHOW GRANTS FOR an account without a grant row)
1147There is no such grant defined for the user on a table
1133The user table has no row that matches (GRANT to an unknown account)
1396CREATE USER / DROP USER failed (with the reason)
1410You are not allowed to create a user with GRANT
1221Incorrect usage of global privileges and a database or table level in one statement
1144Illegal GRANT/REVOKE command
1827The password hash does not have the expected format

SHOW GRANTS#

A bootstrap administrator answers GRANT ALL PRIVILEGES ON *.* TO 'name'@'host' WITH GRANT OPTION. A stored account lists its grants in MySQL's order (global first, then databases, then tables) in MySQL's canonical text, with ALL PRIVILEGES when it has them all. SHOW GRANTS FOR another account needs SELECT on mysql or CREATE USER. The layout of a column grant combined with whole-table privileges is the front-end's own and has not been compared with MySQL.

Examples#

From the privilege checks:

SQL
CREATE USER 'bob'@'%' IDENTIFIED BY 'bobpw';
CREATE USER 'cy'@'%' IDENTIFIED BY 'cypw';
GRANT SELECT, INSERT ON shop.* TO 'bob'@'%';
GRANT ALL PRIVILEGES ON other.* TO 'ann'@'%' WITH GRANT OPTION;
GRANT SELECT (id, status) ON shop.orders TO 'cy'@'%';
GRANT UPDATE (status) ON shop.orders TO 'cy'@'%';
GRANT SELECT ON shop.items TO 'dee'@'%';
SHOW GRANTS FOR 'bob'@'%';

Then, logged in as bob: SELECT * FROM shop.orders works; SELECT * FROM other.secret is error 1142 (or 1044 for USE other); DROP TABLE shop.orders is denied because bob has no DROP. Logged in as cy, SELECT id, status FROM shop.orders works and SELECT * FROM shop.orders is error 1143 for the column total.

Remarks#

  • The front-end checks, the server does not. The front-end reaches the server as one account, so a client of the native API that has that account's password sees everything. Keep that account's password secret and the server's port closed.
  • Grants made with % in a database name match databases created later.
  • Known gaps (D54): mysql.user and mysql.db are not tables; dynamic global privileges; SHOW GRANTS of a column grant mixed with table privileges is not compared with MySQL.

See also#