Bug #121925 SQL SECURITY DEFINER view: error 1143 for definer on JOIN column when invoker has the view privilege via an active role
Description:
A view created with SQL SECURITY DEFINER fails with ERROR 1143 when queried by
a user whose SELECT privilege on the view comes from an active role. The error
names the DEFINER account and the column used in the view's JOIN ... ON
condition, although the definer has table-level SELECT on both base tables.
Under SQL SECURITY DEFINER, privileges on the underlying tables should be
checked against the definer only; the invoker needs only SELECT on the view.
Instead, the result depends on the invoker's session, while the definer and
its grants are identical in every case:
- Invoker has SELECT on the view via an active default role -> ERROR 1143
- Invoker has SELECT on the view directly, SET ROLE NONE -> 1 row
- Invoker is root -> 1 row
Further observations:
- Granting the definer SELECT at database level (GRANT SELECT ON repro_db.*)
makes the failing case work; revoking it brings the error back. The
table-level grants alone should be sufficient.
- The column reported is always the JOIN ON column (table_a.table_b_id), never
the select-list column table_a.id, although the same table-level grant
covers both.
- ALGORITHM=TEMPTABLE gives the same error.
- Definer host '%' vs 'localhost' and locked vs unlocked definer account make
no difference.
- activate_all_roles_on_login = <OFF>, mandatory_roles = '<empty>'.
This differs from Bug #104732: there the definer's privileges came from
roles. Here the definer has no roles at all; only the invoker uses a role.
Impact: the view is meant to expose a limited dataset to a restricted
account under least privilege. The only working configurations are giving the
definer schema-wide SELECT (any object later created with that definer can
read every table in the schema) or not using roles for the invoker.
Reproduced on:
- MySQL Community 8.0.39 (official Docker image)
- MySQL Community 8.4.<x> (official Docker image)
- Amazon Aurora MySQL 3.08.0 (MySQL 8.0.39 compatible)
How to repeat:
-- =========== Session 1: root ===========
SELECT @@version, @@activate_all_roles_on_login, @@mandatory_roles;
CREATE DATABASE repro_db;
CREATE TABLE repro_db.table_b (
id INT UNSIGNED NOT NULL PRIMARY KEY
) ENGINE=InnoDB;
CREATE TABLE repro_db.table_a (
id INT UNSIGNED NOT NULL PRIMARY KEY,
table_b_id INT UNSIGNED NOT NULL,
CONSTRAINT fk_table_a_table_b FOREIGN KEY (table_b_id)
REFERENCES repro_db.table_b (id)
) ENGINE=InnoDB;
INSERT INTO repro_db.table_b (id) VALUES (1);
INSERT INTO repro_db.table_a (id, table_b_id) VALUES (1, 1);
-- Definer: table-level SELECT only, no roles
CREATE USER 'repro_definer'@'%' IDENTIFIED BY 'ABC';
GRANT SELECT ON repro_db.table_a TO 'repro_definer'@'%';
GRANT SELECT ON repro_db.table_b TO 'repro_definer'@'%';
CREATE DEFINER='repro_definer'@'%' SQL SECURITY DEFINER
VIEW repro_db.repro_view AS
SELECT a.id AS a_id, b.id AS b_id
FROM repro_db.table_a a
INNER JOIN repro_db.table_b b ON b.id = a.table_b_id;
-- Invoker: SELECT on the view only, via a default role
CREATE ROLE 'repro_reader_role';
GRANT SELECT ON repro_db.repro_view TO 'repro_reader_role';
CREATE USER 'repro_reader'@'%' IDENTIFIED BY 'ABC'
DEFAULT ROLE 'repro_reader_role';
SELECT * FROM repro_db.repro_view; -- OK, 1 row
-- =========== Session 2: new connection as repro_reader ===========
SELECT CURRENT_USER(), CURRENT_ROLE();
-- repro_reader@% `repro_reader_role`@`%`
SELECT * FROM repro_db.repro_view;
-- ERROR 1143 (42000): SELECT command denied to user 'repro_definer'@'%'
-- for column 'table_b_id' in table 'table_a'
-- EXPECTED: 1 row (a_id=1, b_id=1)
-- =========== Variant A: direct grant, no active role -> works ===========
-- root:
GRANT SELECT ON repro_db.repro_view TO 'repro_reader'@'%';
-- repro_reader:
SET ROLE NONE;
SELECT * FROM repro_db.repro_view; -- OK, 1 row
-- root:
REVOKE SELECT ON repro_db.repro_view FROM 'repro_reader'@'%';
-- =========== Variant B: database-level grant on definer -> works ===========
-- root:
GRANT SELECT ON repro_db.* TO 'repro_definer'@'%';
-- repro_reader (new connection, role active):
SELECT * FROM repro_db.repro_view; -- OK, 1 row
-- root:
REVOKE SELECT ON repro_db.* FROM 'repro_definer'@'%';
-- repro_reader (new connection): ERROR 1143 again
-- =========== Variant C: ALGORITHM=TEMPTABLE -> same error ===========
-- root:
CREATE OR REPLACE ALGORITHM=TEMPTABLE
DEFINER='repro_definer'@'%' SQL SECURITY DEFINER
VIEW repro_db.repro_view AS
SELECT a.id AS a_id, b.id AS b_id
FROM repro_db.table_a a
INNER JOIN repro_db.table_b b ON b.id = a.table_b_id;
-- repro_reader (new connection, role active): ERROR 1143
-- =========== Cleanup (root) ===========
DROP VIEW repro_db.repro_view;
DROP USER 'repro_reader'@'%';
DROP ROLE 'repro_reader_role';
DROP USER 'repro_definer'@'%';
DROP DATABASE repro_db;
Suggested fix:
No fix known. Hypothesis from the observations: when the invoker has active
roles, the definer security context used for the view's column-level
privilege check does not resolve the definer's table-level grants, while
database-level grants are still found. Possibly the same role-handling code
path as Bug #104732, but with the roles on the invoker side.
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.