summaryrefslogtreecommitdiff
path: root/sql/item.cc
diff options
context:
space:
mode:
authorAlexander Barkov <bar@mariadb.com>2022-04-09 23:01:26 +0400
committerAlexander Barkov <bar@mariadb.com>2022-04-09 23:01:26 +0400
commit2ae92e89817b1eceed134fcf98e21f99ce26a22d (patch)
tree2a4f0c0e916adc01a0587dfd57a043fac5699a75 /sql/item.cc
parentd623b5a1dd9196fdeb60d933a51f48967c9d4196 (diff)
downloadmariadb-git-2ae92e89817b1eceed134fcf98e21f99ce26a22d.tar.gz
MDEV-28267 ASAN heap-use-after-free in Item_sp::func_name_cstring
This crash happens on a combination of multiple conditions: - There is a thead#1 running an "ANALYZE FORMAT=JSON" query for a "SELECT .. FROM INFORMATION_SCHEMA.COLUMNS WHERE .. " - The WHERE clause contains a stored function call, say f1(). - The WHERE clause is built in the way so that the function f1() is never actually called, e.g. WHERE .. AND (TRUE OR f1()=expr) - The database contains multiple VIEWs that have the function f1() call, e.g. in their <select list> - The WHERE clause is built in the way so that these VIEWs match the condition. - There is a parallel thread#2 running. It creates or drops or recreates some other stored routine, say f2(), which is not used in the ANALYZE query. It effectively invalidates the stored routine cache for thread#1 without locking. Note, it is important that f2() is NOT used by ANALYZE query. Otherwise, thread#2 would be locked until the ANALYZE query finishes. When all of the above conditions are met, the following happens: 1. thread#1 starts the ANALYZE query. It notices a call for the stored function f1() in the WHERE condition. The function f1() gets parsed and cached to the SP cache. Its address also gets assigned to Item_func_sp::m_sp. 2. thread#1 starts iterating through all tables that match the WHERE condition to find the information about their columns. 3. thread#1 processes columns of the VIEW v1. It notices a call for f1() in the VIEW v1 definition. But f1() is already cached in the step#1 and it is up to date. So nothing happens with the SP cache. 4. thread#2 re-creates f2() in a non-locking mode. It effectively invalidates the SP cache in thread#1. 5. thread#1 processes columns of the VIEW v2. It notices a call for f1() in the VIEW v2 definition. It also notices that the cached version of f1() is not up to date. It frees the old definition of f1(), parses it again, and puts a new version of f1() to the SP cache. 6. thread#1 finishes processing rows and generates the JSON output. When printing the "attached_condition" value, it calls Item_func_sp::print() for f1(). But this Item_func_sp links to the old (freed) version of f1(). The above scenario demonstrates that Item_func_sp::m_sp can point to an alredy freed instance when Item_func_sp::func_name() is called, so accessing to Item_sp::m_sp->m_handler is not safe. This patch rewrites the code to use Item_func_sp::m_handler instead, which is always reliable. Note, this patch is only a cleanup for MDEV-28166 to quickly fix the regression. It fixes MDEV-28267. But it does not fix the core problem: The code behind I_S does not take into account that the SP cache can be updated while evaluating rows of the COLUMNS table. This is a corner case and it never happens with any other tables. I_S.COLUMNS is very special. Another example of the core problem is reported in MDEV-25243. The code accesses to Item_sp::m_sp->m_chistics of an already freed m_sp, again. It will be addressed separately.
Diffstat (limited to 'sql/item.cc')
-rw-r--r--sql/item.cc4
1 files changed, 2 insertions, 2 deletions
diff --git a/sql/item.cc b/sql/item.cc
index f06055f0a08..8117199f01a 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -2893,7 +2893,7 @@ Item_sp::Item_sp(THD *thd, Item_sp *item):
}
const char *
-Item_sp::func_name(THD *thd) const
+Item_sp::func_name(THD *thd, bool is_package_function) const
{
/* Calculate length to avoid reallocation of string for sure */
size_t len= (((m_name->m_explicit_name ? m_name->m_db.length : 0) +
@@ -2914,7 +2914,7 @@ Item_sp::func_name(THD *thd) const
append_identifier(thd, &qname, &m_name->m_db);
qname.append('.');
}
- if (m_sp && m_sp->m_handler == &sp_handler_package_function)
+ if (is_package_function)
{
/*
In case of a package function split `pkg.func` and print