Bug #121803 PARTITIONS 4294967298 creates 2 partitions: partition counts are truncated to 32 bits before the 8192 limit check

Submitted: 17 Nov 7:25
Reporter: Ke Han Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Partitions Severity:S3 (Non-critical)
Version:26.10.0 (also 9.7.4, 8.4.13) OS:Any
Assigned to: CPU Architecture:Any

[17 Nov 7:25] Ke Han
Description:
A table can have at most 8192 partitions including subpartitions ("The
maximum possible number of partitions for a given table not using the NDB
storage engine is 8192. This number includes subpartitions.", 26.6
Restrictions and Limitations on Partitioning).  PARTITIONS 8193 and
PARTITIONS 2147483649 are rejected with ER_TOO_MANY_PARTITIONS_ERROR
(1499).  But the count is truncated to 32 bits before it is checked, so a
count of 2^32 or more is accepted and the table gets the low 32 bits of
the count:

  - PARTITIONS 4294967298 (2^32 + 2) creates a table with 2 partitions;
  - PARTITIONS 4294967296 (2^32) creates a table with 1 partition: the
    truncated count is 0, and 0 means "no PARTITIONS clause";
  - SUBPARTITIONS 4294967299 creates 3 subpartitions per partition;
  - ALTER TABLE ... ADD PARTITION PARTITIONS 4294967297 adds 1 partition;
  - ALTER TABLE ... COALESCE PARTITION 4294967297 removes 1 partition.

No warning is given.

Cause: the grammar reads the count as real_ulong_num (a 64-bit ulong on
64-bit Linux) and checks only for 0 there:

  sql/sql_yacc.yy:6359-6370 (opt_num_parts; opt_num_subparts at :6408)
  https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/sq...

    | PARTITIONS_SYM real_ulong_num
      {
        if ($2 == 0) { my_error(ER_NO_PARTS_ERROR, ...); MYSQL_YYABORT; }
        $$= $2;
      }

It then passes the value to parse-tree constructors whose parameter is a
32-bit uint, which truncates it:

  sql/parse_tree_partitions.h:639   PT_partition(..., uint opt_num_parts, ...)
  sql/parse_tree_partitions.h:461   PT_sub_partition_by_hash(..., uint opt_num_subparts)
  sql/parse_tree_partitions.h:487   PT_sub_partition_by_key(..., const uint opt_num_subparts)
  sql/parse_tree_nodes.h:4779       PT_alter_table_add_partition_num(..., uint num_parts)
  sql/parse_tree_nodes.h:4945       PT_alter_table_coalesce_partition(..., uint num_parts)
  https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/pa...
  https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/pa...

(called from sql_yacc.yy:6305, :6377, :6382, :8989 and :9024).
partition_info::num_parts / num_subparts are uint as well
(sql/partition_info.h:370-371), so the MAX_PARTITIONS (8192) checks in
sql/partition_info.cc:750, :812 and :1545 see only the truncated value.
sql/parse_tree_partitions.cc:486 treats a truncated 0 as "no count given":

    if (opt_num_parts != 0) {
      part_info.num_parts = opt_num_parts;

The same narrowing is in two account options.  "PASSWORD HISTORY n" and
"PASSWORD REUSE INTERVAL n DAY" assign real_ulong_num to the uint32 fields
password_history_length / password_reuse_interval (sql/sql_yacc.yy:8745 and
:8759; sql/table.h:2788 and :2791).  65536 .. 4294967295 end up as 65535
(the columns are SMALLINT UNSIGNED), but 2^32 and above wrap:
PASSWORD HISTORY 4294967299 stores 3, and PASSWORD HISTORY 4294967296
stores 0, which turns the history check off for the account.
FAILED_LOGIN_ATTEMPTS 4294967299, PASSWORD_LOCK_TIME 4294967299 and
PASSWORD EXPIRE INTERVAL 4294967299 DAY are rejected with ERROR 1525, and
SET GLOBAL password_history = 4294967299 is clamped to 4294967295 with
warning 1292.

  https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/sq...

How to repeat:
CREATE DATABASE IF NOT EXISTS test;
USE test;

CREATE TABLE t1 (a INT) PARTITION BY HASH(a) PARTITIONS 8193;
CREATE TABLE t1 (a INT) PARTITION BY HASH(a) PARTITIONS 2147483649;
CREATE TABLE t1 (a INT) PARTITION BY HASH(a) PARTITIONS 4294967298;
SELECT GROUP_CONCAT(PARTITION_NAME ORDER BY PARTITION_NAME) AS parts
FROM information_schema.PARTITIONS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't1';
DROP TABLE t1;

CREATE TABLE t1 (a INT) PARTITION BY KEY(a) PARTITIONS 4294967296;
SELECT GROUP_CONCAT(PARTITION_NAME ORDER BY PARTITION_NAME) AS parts
FROM information_schema.PARTITIONS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't1';
DROP TABLE t1;

CREATE TABLE t1 (a INT, b INT) PARTITION BY RANGE(a)
  SUBPARTITION BY HASH(b) SUBPARTITIONS 4294967299
  (PARTITION p0 VALUES LESS THAN (10), PARTITION p1 VALUES LESS THAN MAXVALUE);
SELECT GROUP_CONCAT(SUBPARTITION_NAME ORDER BY SUBPARTITION_NAME) AS subparts
FROM information_schema.PARTITIONS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't1';
DROP TABLE t1;

CREATE TABLE t1 (a INT) PARTITION BY HASH(a) PARTITIONS 2;
ALTER TABLE t1 ADD PARTITION PARTITIONS 9000;
ALTER TABLE t1 ADD PARTITION PARTITIONS 4294967297;
SELECT GROUP_CONCAT(PARTITION_NAME ORDER BY PARTITION_NAME) AS parts
FROM information_schema.PARTITIONS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't1';
ALTER TABLE t1 COALESCE PARTITION 4294967297;
SELECT GROUP_CONCAT(PARTITION_NAME ORDER BY PARTITION_NAME) AS parts
FROM information_schema.PARTITIONS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't1';
DROP TABLE t1;

CREATE USER u1@localhost PASSWORD HISTORY 4294967299
  PASSWORD REUSE INTERVAL 4294967299 DAY;
CREATE USER u2@localhost PASSWORD HISTORY 4294967296;
CREATE USER u3@localhost PASSWORD HISTORY 70000
  PASSWORD REUSE INTERVAL 70000 DAY;
SELECT user, Password_reuse_history, Password_reuse_time FROM mysql.user
WHERE user IN ('u1', 'u2', 'u3') ORDER BY user;
DROP USER u1@localhost, u2@localhost, u3@localhost;

Output on 26.10.0 (mysql --table -vv < script; the "-----" separator lines
removed):

CREATE DATABASE IF NOT EXISTS test

Query OK, 1 row affected, 1 warning

USE test

Query OK, 0 rows affected

CREATE TABLE t1 (a INT) PARTITION BY HASH(a) PARTITIONS 8193

ERROR 1499 (HY000) at line 4: Too many partitions (including subpartitions) were defined
CREATE TABLE t1 (a INT) PARTITION BY HASH(a) PARTITIONS 2147483649

ERROR 1499 (HY000) at line 5: Too many partitions (including subpartitions) were defined
CREATE TABLE t1 (a INT) PARTITION BY HASH(a) PARTITIONS 4294967298

Query OK, 0 rows affected

SELECT GROUP_CONCAT(PARTITION_NAME ORDER BY PARTITION_NAME) AS parts
FROM information_schema.PARTITIONS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't1'

+-------+
| parts |
+-------+
| p0,p1 |
+-------+
1 row in set

DROP TABLE t1

Query OK, 0 rows affected

CREATE TABLE t1 (a INT) PARTITION BY KEY(a) PARTITIONS 4294967296

Query OK, 0 rows affected

SELECT GROUP_CONCAT(PARTITION_NAME ORDER BY PARTITION_NAME) AS parts
FROM information_schema.PARTITIONS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't1'

+-------+
| parts |
+-------+
| p0    |
+-------+
1 row in set

DROP TABLE t1

Query OK, 0 rows affected

CREATE TABLE t1 (a INT, b INT) PARTITION BY RANGE(a)
  SUBPARTITION BY HASH(b) SUBPARTITIONS 4294967299
  (PARTITION p0 VALUES LESS THAN (10), PARTITION p1 VALUES LESS THAN MAXVALUE)

Query OK, 0 rows affected

SELECT GROUP_CONCAT(SUBPARTITION_NAME ORDER BY SUBPARTITION_NAME) AS subparts
FROM information_schema.PARTITIONS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't1'

+-------------------------------------+
| subparts                            |
+-------------------------------------+
| p0sp0,p0sp1,p0sp2,p1sp0,p1sp1,p1sp2 |
+-------------------------------------+
1 row in set

DROP TABLE t1

Query OK, 0 rows affected

CREATE TABLE t1 (a INT) PARTITION BY HASH(a) PARTITIONS 2

Query OK, 0 rows affected

ALTER TABLE t1 ADD PARTITION PARTITIONS 9000

ERROR 1499 (HY000) at line 27: Too many partitions (including subpartitions) were defined
ALTER TABLE t1 ADD PARTITION PARTITIONS 4294967297

Query OK, 0 rows affected
Records: 0  Duplicates: 0  Warnings: 0

SELECT GROUP_CONCAT(PARTITION_NAME ORDER BY PARTITION_NAME) AS parts
FROM information_schema.PARTITIONS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't1'

+----------+
| parts    |
+----------+
| p0,p1,p2 |
+----------+
1 row in set

ALTER TABLE t1 COALESCE PARTITION 4294967297

Query OK, 0 rows affected
Records: 0  Duplicates: 0  Warnings: 0

SELECT GROUP_CONCAT(PARTITION_NAME ORDER BY PARTITION_NAME) AS parts
FROM information_schema.PARTITIONS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't1'

+-------+
| parts |
+-------+
| p0,p1 |
+-------+
1 row in set

DROP TABLE t1

Query OK, 0 rows affected

CREATE USER u1@localhost PASSWORD HISTORY 4294967299
  PASSWORD REUSE INTERVAL 4294967299 DAY

Query OK, 0 rows affected

CREATE USER u2@localhost PASSWORD HISTORY 4294967296

Query OK, 0 rows affected

CREATE USER u3@localhost PASSWORD HISTORY 70000
  PASSWORD REUSE INTERVAL 70000 DAY

Query OK, 0 rows affected

SELECT user, Password_reuse_history, Password_reuse_time FROM mysql.user
WHERE user IN ('u1', 'u2', 'u3') ORDER BY user

+------+------------------------+---------------------+
| user | Password_reuse_history | Password_reuse_time |
+------+------------------------+---------------------+
| u1   |                      3 |                   3 |
| u2   |                      0 |                NULL |
| u3   |                  65535 |               65535 |
+------+------------------------+---------------------+
3 rows in set

DROP USER u1@localhost, u2@localhost, u3@localhost

Query OK, 0 rows affected

Bye

The truncated PASSWORD HISTORY is what the server enforces: with a global
history of 5, an account created with PASSWORD HISTORY 4294967296 may
reuse its current password, an account without the clause may not:

SET GLOBAL password_history = 5;
CREATE USER u8@localhost IDENTIFIED BY 'Secret-1' PASSWORD HISTORY 4294967296;
ALTER USER u8@localhost IDENTIFIED BY 'Secret-1';
CREATE USER u9@localhost IDENTIFIED BY 'Secret-1';
ALTER USER u9@localhost IDENTIFIED BY 'Secret-1';
SET GLOBAL password_history = 0;
DROP USER u8@localhost, u9@localhost;

Output on 26.10.0 (same way):

SET GLOBAL password_history = 5

Query OK, 0 rows affected

CREATE USER u8@localhost IDENTIFIED BY 'Secret-1' PASSWORD HISTORY 4294967296

Query OK, 0 rows affected

ALTER USER u8@localhost IDENTIFIED BY 'Secret-1'

Query OK, 0 rows affected

CREATE USER u9@localhost IDENTIFIED BY 'Secret-1'

Query OK, 0 rows affected

ALTER USER u9@localhost IDENTIFIED BY 'Secret-1'

ERROR 3638 (HY000) at line 5: Cannot use these credentials for 'u9@localhost' because they contradict the password history policy
SET GLOBAL password_history = 0

Query OK, 0 rows affected

DROP USER u8@localhost, u9@localhost

Query OK, 0 rows affected

Bye

Expected: ERROR 1499 for every count above 8192, as for 8193 and
2147483649 (and not a silent fall-back to one partition for 2^32).  For
PASSWORD HISTORY / PASSWORD REUSE INTERVAL: an error, or at least the same
clamping to 65535 as for 70000, not the low 32 bits of the value.

Suggested fix:
Check the range on the 64-bit value before it is narrowed, in the grammar
actions of opt_num_parts / opt_num_subparts and of the ADD PARTITION
PARTITIONS / COALESCE PARTITION rules:

    if ($2 == 0) { ... ER_NO_PARTS_ERROR ... }
    if ($2 > MAX_PARTITIONS)
    {
      my_error(ER_TOO_MANY_PARTITIONS_ERROR, MYF(0));
      MYSQL_YYABORT;
    }

or make the parse-tree parameters ulong and compare against MAX_PARTITIONS
before narrowing.  For PASSWORD HISTORY / PASSWORD REUSE INTERVAL, reject
(or clamp) values above UINT_MAX16 in the grammar before the assignment, as
is already done for FAILED_LOGIN_ATTEMPTS / PASSWORD_LOCK_TIME.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.049 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.