Skip to content

Commit 6aff5fa

Browse files
committed
MDEV-15619 using CONVERT() inside AES_ENCRYPT() in an UPDATE corrupts data
1 parent a2e47f8 commit 6aff5fa

File tree

4 files changed

+90
-6
lines changed

4 files changed

+90
-6
lines changed

mysql-test/r/func_str.result

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4551,3 +4551,40 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
45514551
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL No tables used
45524552
Warnings:
45534553
Note 1003 select char(0xdf) AS `CHAR(0xDF)`
4554+
#
4555+
# MDEV-15619 using CONVERT() inside AES_ENCRYPT() in an UPDATE corrupts data
4556+
#
4557+
CREATE TABLE t1 (
4558+
id int(11) NOT NULL,
4559+
session_id varchar(255) DEFAULT NULL,
4560+
directory mediumtext,
4561+
checksum int(10) DEFAULT NULL,
4562+
last_update datetime DEFAULT NULL,
4563+
PRIMARY KEY (id),
4564+
KEY lastupdate (last_update)
4565+
) DEFAULT CHARSET=latin1;
4566+
INSERT INTO t1 VALUES (1,'',NULL,38391,'2017-06-24 07:35:28');
4567+
UPDATE t1 SET directory = AES_ENCRYPT(CONVERT('test stringrererejrjerjehrjekhrjkehrjkehrkjehrjkerhkjehrjekrhkjehrkjerhjkehrkjehrkjehrjkehrjkehrjkehrjkerjkehrjkehrjkehrjke rekjhrejrejhrjehgrehjgrhjerjhegrjherejhgrjhegrjehgrjhegrejhrgjehgrjhegrjhegrjhergjhegrjhegrhjegrjerhthkjjkdhjkgdfjkgjkdgdjkfjkhgjkfdhjgjkfdghkjdfghkjfdghfjkdghkdjfghdkjfghfjkdghfkjdghkjfdghfkjdghfkdjghfkjdghfdjkghjkdfhgdfjkghfjkdghfjkdghfjdkghfjkdghkfjdghfkjdghfkjdghkjdfghfjdkghjkfdghkjdfhgjkdfhgjkfdhgkjfdghkfjdhgkjfdgdjkejktjherjthkjrethkjrethjkerthjkerhtjkerhtkjerhtjkerhtjkerhtjkrehtkjerhtkjrehtjkrehtkjrehtkjerhtkjerhtjkrehtkjrehtjkrehtkjrethjkrethkjrehtkjethjkerhtjkrehtjkretkjerhtkjrehtjkerhtjkrehtjrehtkjrekjtrfgdsfgdhjsghjgfdhjsfhjdfgdhjshjdshjfghjdsfgjhsfgjhsdfgjhdsfgjdhsfgsjhfgjhsdfgsdjhfgjdhsfdjshfgdsjhfgjsdhfdjshfgdjhsfgdjshfgjdhsfgjhsdfgjhsdgfjhsdgfjhdsgfjhsgfjhsdgfjhdsgfhjsdehkjthrkjethjkre' USING latin1), '95F5A1F52A554'), last_update= NOW();
4568+
SELECT directory IS NULL FROM t1;
4569+
directory IS NULL
4570+
0
4571+
DROP TABLE t1;
4572+
CREATE TABLE t1 (
4573+
id int(11) NOT NULL PRIMARY KEY,
4574+
directory mediumtext
4575+
) DEFAULT CHARSET=latin1;
4576+
INSERT INTO t1 VALUES (1,AES_ENCRYPT(CONVERT(REPEAT('a',800) USING latin1),'95F5A1F52A554'));
4577+
SELECT AES_DECRYPT(directory,'95F5A1F52A554') FROM t1;
4578+
AES_DECRYPT(directory,'95F5A1F52A554')
4579+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
4580+
DROP TABLE t1;
4581+
SET @enc=AES_ENCRYPT(REPEAT(_latin1'a',800),'95F5A1F52A554');
4582+
CREATE TABLE t1 (
4583+
id int(11) NOT NULL PRIMARY KEY,
4584+
directory mediumtext
4585+
) DEFAULT CHARSET=latin1;
4586+
INSERT INTO t1 VALUES (1,AES_DECRYPT(CONVERT(@enc USING binary),'95F5A1F52A554'));
4587+
SELECT * FROM t1;
4588+
id directory
4589+
1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
4590+
DROP TABLE t1;

mysql-test/t/func_str.test

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,3 +1754,39 @@ EXECUTE stmt;
17541754
EXPLAIN EXTENDED SELECT CHAR(0xDF USING latin1);
17551755
EXPLAIN EXTENDED SELECT CHAR(0xDF USING `binary`);
17561756
EXPLAIN EXTENDED SELECT CHAR(0xDF);
1757+
1758+
1759+
--echo #
1760+
--echo # MDEV-15619 using CONVERT() inside AES_ENCRYPT() in an UPDATE corrupts data
1761+
--echo #
1762+
1763+
CREATE TABLE t1 (
1764+
id int(11) NOT NULL,
1765+
session_id varchar(255) DEFAULT NULL,
1766+
directory mediumtext,
1767+
checksum int(10) DEFAULT NULL,
1768+
last_update datetime DEFAULT NULL,
1769+
PRIMARY KEY (id),
1770+
KEY lastupdate (last_update)
1771+
) DEFAULT CHARSET=latin1;
1772+
INSERT INTO t1 VALUES (1,'',NULL,38391,'2017-06-24 07:35:28');
1773+
UPDATE t1 SET directory = AES_ENCRYPT(CONVERT('test stringrererejrjerjehrjekhrjkehrjkehrkjehrjkerhkjehrjekrhkjehrkjerhjkehrkjehrkjehrjkehrjkehrjkehrjkerjkehrjkehrjkehrjke rekjhrejrejhrjehgrehjgrhjerjhegrjherejhgrjhegrjehgrjhegrejhrgjehgrjhegrjhegrjhergjhegrjhegrhjegrjerhthkjjkdhjkgdfjkgjkdgdjkfjkhgjkfdhjgjkfdghkjdfghkjfdghfjkdghkdjfghdkjfghfjkdghfkjdghkjfdghfkjdghfkdjghfkjdghfdjkghjkdfhgdfjkghfjkdghfjkdghfjdkghfjkdghkfjdghfkjdghfkjdghkjdfghfjdkghjkfdghkjdfhgjkdfhgjkfdhgkjfdghkfjdhgkjfdgdjkejktjherjthkjrethkjrethjkerthjkerhtjkerhtkjerhtjkerhtjkerhtjkrehtkjerhtkjrehtjkrehtkjrehtkjerhtkjerhtjkrehtkjrehtjkrehtkjrethjkrethkjrehtkjethjkerhtjkrehtjkretkjerhtkjrehtjkerhtjkrehtjrehtkjrekjtrfgdsfgdhjsghjgfdhjsfhjdfgdhjshjdshjfghjdsfgjhsfgjhsdfgjhdsfgjdhsfgsjhfgjhsdfgsdjhfgjdhsfdjshfgdsjhfgjsdhfdjshfgdjhsfgdjshfgjdhsfgjhsdfgjhsdgfjhsdgfjhdsgfjhsgfjhsdgfjhdsgfhjsdehkjthrkjethjkre' USING latin1), '95F5A1F52A554'), last_update= NOW();
1774+
SELECT directory IS NULL FROM t1;
1775+
DROP TABLE t1;
1776+
1777+
CREATE TABLE t1 (
1778+
id int(11) NOT NULL PRIMARY KEY,
1779+
directory mediumtext
1780+
) DEFAULT CHARSET=latin1;
1781+
INSERT INTO t1 VALUES (1,AES_ENCRYPT(CONVERT(REPEAT('a',800) USING latin1),'95F5A1F52A554'));
1782+
SELECT AES_DECRYPT(directory,'95F5A1F52A554') FROM t1;
1783+
DROP TABLE t1;
1784+
1785+
SET @enc=AES_ENCRYPT(REPEAT(_latin1'a',800),'95F5A1F52A554');
1786+
CREATE TABLE t1 (
1787+
id int(11) NOT NULL PRIMARY KEY,
1788+
directory mediumtext
1789+
) DEFAULT CHARSET=latin1;
1790+
INSERT INTO t1 VALUES (1,AES_DECRYPT(CONVERT(@enc USING binary),'95F5A1F52A554'));
1791+
SELECT * FROM t1;
1792+
DROP TABLE t1;

sql/item_strfunc.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ String *Item_func_aes_encrypt::val_str(String *str2)
379379
DBUG_ASSERT(fixed == 1);
380380
char key_buff[80];
381381
String tmp_key_value(key_buff, sizeof(key_buff), system_charset_info);
382-
String *sptr= args[0]->val_str(&str_value); // String to encrypt
382+
String *sptr= args[0]->val_str(&tmp_value); // String to encrypt
383383
String *key= args[1]->val_str(&tmp_key_value); // key
384384
int aes_length;
385385
if (sptr && key) // we need both arguments to be not NULL
@@ -418,7 +418,7 @@ String *Item_func_aes_decrypt::val_str(String *str)
418418
String *sptr, *key;
419419
DBUG_ENTER("Item_func_aes_decrypt::val_str");
420420

421-
sptr= args[0]->val_str(&str_value); // String to decrypt
421+
sptr= args[0]->val_str(&tmp_value); // String to decrypt
422422
key= args[1]->val_str(&tmp_key_value); // Key
423423
if (sptr && key) // Need to have both arguments not NULL
424424
{

sql/item_strfunc.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,30 @@ class Item_func_from_base64 :public Item_str_func
134134
};
135135

136136

137-
class Item_func_aes_encrypt :public Item_str_func
137+
class Item_aes_crypt :public Item_str_func
138+
{
139+
protected:
140+
String tmp_value;
141+
public:
142+
Item_aes_crypt(Item *a, Item *b)
143+
:Item_str_func(a, b) {}
144+
};
145+
146+
class Item_func_aes_encrypt :public Item_aes_crypt
138147
{
139148
public:
140-
Item_func_aes_encrypt(Item *a, Item *b) :Item_str_func(a,b) {}
149+
Item_func_aes_encrypt(Item *a, Item *b):
150+
Item_aes_crypt(a, b) {}
141151
String *val_str(String *);
142152
void fix_length_and_dec();
143153
const char *func_name() const { return "aes_encrypt"; }
144154
};
145155

146-
class Item_func_aes_decrypt :public Item_str_func
156+
class Item_func_aes_decrypt :public Item_aes_crypt
147157
{
148158
public:
149-
Item_func_aes_decrypt(Item *a, Item *b) :Item_str_func(a,b) {}
159+
Item_func_aes_decrypt(Item *a, Item *b):
160+
Item_aes_crypt(a,b) {}
150161
String *val_str(String *);
151162
void fix_length_and_dec();
152163
const char *func_name() const { return "aes_decrypt"; }

0 commit comments

Comments
 (0)