Skip to content

Commit d193728

Browse files
Dmitry LenevHery Ramilison
authored andcommitted
Fix for bug#26106655 "DISCREPANCY BETWEEN IMPLICIT DB OF PARENT TABLE FOR FK AND REFERENCES PRIVILEGE".
Check for REFERENCES privilege was using incorrect database in some cases. There was discrepancy between database which was used by SEs for parent tables and used for check of REFERENCES privilege in cases when no explicit database was specified for parent table. This patch removes the discrepancy by aligning check for REFERENCES privilege with SE behavior. (cherry picked from commit bd6aa24131505886b06381fcce97d97efa9f6002)
1 parent 69f8305 commit d193728

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

sql/auth/auth_common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef AUTH_COMMON_INCLUDED
22
#define AUTH_COMMON_INCLUDED
33

4-
/* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
4+
/* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
55
66
This program is free software; you can redistribute it and/or modify
77
it under the terms of the GNU General Public License as published by
@@ -695,6 +695,7 @@ bool lock_tables_precheck(THD *thd, TABLE_LIST *tables);
695695
bool create_table_precheck(THD *thd, TABLE_LIST *tables,
696696
TABLE_LIST *create_table);
697697
bool check_fk_parent_table_access(THD *thd,
698+
const char *child_table_db,
698699
HA_CREATE_INFO *create_info,
699700
Alter_info *alter_info);
700701
bool check_readonly(THD *thd, bool err_if_readonly);

sql/auth/sql_authorization.cc

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
1+
/* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
22
33
This program is free software; you can redistribute it and/or modify
44
it under the terms of the GNU General Public License as published by
@@ -464,7 +464,8 @@ bool create_table_precheck(THD *thd, TABLE_LIST *tables,
464464
goto err;
465465
}
466466

467-
if (check_fk_parent_table_access(thd, &lex->create_info, &lex->alter_info))
467+
if (check_fk_parent_table_access(thd, create_table->db,
468+
&lex->create_info, &lex->alter_info))
468469
goto err;
469470

470471
error= FALSE;
@@ -4329,10 +4330,11 @@ bool check_global_access(THD *thd, ulong want_access)
43294330
/**
43304331
Checks foreign key's parent table access.
43314332
4332-
@param thd [in] Thread handler
4333-
@param create_info [in] Create information (like MAX_ROWS, ENGINE or
4333+
@param thd [in] Thread handler
4334+
@param child_table_db [in] Database of child table
4335+
@param create_info [in] Create information (like MAX_ROWS, ENGINE or
43344336
temporary table flag)
4335-
@param alter_info [in] Initial list of columns and indexes for the
4337+
@param alter_info [in] Initial list of columns and indexes for the
43364338
table to be created
43374339
43384340
@retval
@@ -4341,6 +4343,7 @@ bool check_global_access(THD *thd, ulong want_access)
43414343
true error or access denied. Error is sent to client in this case.
43424344
*/
43434345
bool check_fk_parent_table_access(THD *thd,
4346+
const char *child_table_db,
43444347
HA_CREATE_INFO *create_info,
43454348
Alter_info *alter_info)
43464349
{
@@ -4383,10 +4386,17 @@ bool check_fk_parent_table_access(THD *thd,
43834386
if (fk_key->ref_db.str && check_and_convert_db_name(&db_name, false))
43844387
return true;
43854388
}
4386-
else if (thd->lex->copy_db_to(&db_name.str, &db_name.length))
4387-
return true;
43884389
else
4390+
{
4391+
/*
4392+
If database name for parent table is not specified explicitly
4393+
SEs assume that it is the same as database name of child table.
4394+
We do the same here.
4395+
*/
43894396
is_qualified_table_name= false;
4397+
db_name.str= const_cast<char*>(child_table_db);
4398+
db_name.length= strlen(child_table_db);
4399+
}
43904400

43914401
// if lower_case_table_names is set then convert tablename to lower case.
43924402
if (lower_case_table_names)

sql/sql_table.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9237,7 +9237,8 @@ bool mysql_alter_table(THD *thd, const char *new_db, const char *new_name,
92379237
till this point for the alter operation.
92389238
*/
92399239
if ((alter_info->flags & Alter_info::ADD_FOREIGN_KEY) &&
9240-
check_fk_parent_table_access(thd, create_info, alter_info))
9240+
check_fk_parent_table_access(thd, alter_ctx.new_db,
9241+
create_info, alter_info))
92419242
DBUG_RETURN(true);
92429243

92439244
/*

0 commit comments

Comments
 (0)