Skip to content

Commit 49821f2

Browse files
committed
MDEV-9158 post-merge fixes
* format error messages (spaces, "bytes") * speed up "length too large" test from 12s to 70ms * fix it for --parallel * fix "named pipe" test to actually test a named pipe * add the standard header to tests, enable result log * fix for ASAN * read loop to workaround small (64K) pipe buffer size * clarified error message for the file too large
1 parent 4dee592 commit 49821f2

File tree

7 files changed

+88
-56
lines changed

7 files changed

+88
-56
lines changed
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
#
2+
# MDEV-9158 file_key_management should support reading from a named pipe
3+
#
14
# Test checks if opening a too large key file, file key plugin will only read max key file size bytes and extra bytes will be ignored.
2-
#Large key file will read max size bytes , which is 1MB
3-
call mtr.add_suppression("Syntax error");
4-
call mtr.add_suppression("Invalid key");
5+
select seq,repeat(md5(seq), 200)
6+
into outfile 'MYSQLTEST_VARDIR/tmp/filekeys-data-too-large.key'
7+
fields terminated by ';'
8+
from seq_1_to_1000;
9+
#Large key file will read max size bytes, which is 1MB
10+
call mtr.add_suppression("filekeys-data-too-large.key too large, must be less than 1048576 bytes");
511
call mtr.add_suppression("Plugin 'file_key_management' init function returned error");
612
call mtr.add_suppression("Plugin 'file_key_management' registration.*failed");
713
INSTALL SONAME 'file_key_management';
8-
FOUND 1 /read bytes: 1048576B/ in mysqld.1.err
14+
ERROR HY000: File MYSQLTEST_VARDIR/tmp/filekeys-data-too-large.key too large, must be less than 1048576 bytes
15+
FOUND 1 /filekeys-data-too-large.key too large, must be less than 1048576 bytes/ in mysqld.1.err

mysql-test/suite/encryption/r/filekeys_named_pipe.result

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1+
#
2+
# MDEV-9158 file_key_management should support reading from a named pipe
3+
#
4+
select seq,md5(seq)
5+
into outfile 'MYSQLTEST_VARDIR/tmp/fifo-key.txt'
6+
fields terminated by ';'
7+
from seq_1_to_20000;
18
INSTALL SONAME 'file_key_management';
29
Warnings:
3-
Note 1 Read from MYSQL_TMP_DIR/fifo.key , read bytes: 105B, max key file size :1048576B
4-
CREATE TABLE t1(c1 BIGINT NOT NULL, b CHAR(200)) ENGINE=INNODB ENCRYPTED=YES ENCRYPTION_KEY_ID=1;
10+
Note 1 Read from MYSQLTEST_VARDIR/tmp/fifo.key, read bytes: 768894, max key file size: 1048576 bytes
11+
CREATE TABLE t1(c1 BIGINT NOT NULL, b CHAR(200)) ENGINE=INNODB ENCRYPTED=YES ENCRYPTION_KEY_ID=1;
512
SHOW CREATE TABLE t1;
613
Table Create Table
714
t1 CREATE TABLE `t1` (
815
`c1` bigint(20) NOT NULL,
916
`b` char(200) DEFAULT NULL
10-
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci `ENCRYPTED`=YES `ENCRYPTION_KEY_ID`=1
17+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci `ENCRYPTED`=YES `ENCRYPTION_KEY_ID`=1
1118
INSERT t1 VALUES (12345, REPEAT('1234567890', 20));
1219
ALTER TABLE t1 ENCRYPTION_KEY_ID=2;
1320
SHOW CREATE TABLE t1;
1421
Table Create Table
1522
t1 CREATE TABLE `t1` (
1623
`c1` bigint(20) NOT NULL,
1724
`b` char(200) DEFAULT NULL
18-
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci `ENCRYPTED`=YES `ENCRYPTION_KEY_ID`=2
25+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci `ENCRYPTED`=YES `ENCRYPTION_KEY_ID`=2
1926
ALTER TABLE t1 ENCRYPTION_KEY_ID=3;
2027
SHOW CREATE TABLE t1;
2128
Table Create Table
2229
t1 CREATE TABLE `t1` (
2330
`c1` bigint(20) NOT NULL,
2431
`b` char(200) DEFAULT NULL
25-
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci `ENCRYPTED`=YES `ENCRYPTION_KEY_ID`=3
32+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci `ENCRYPTED`=YES `ENCRYPTION_KEY_ID`=3
2633
# Reset mysqld
2734
DROP TABLE t1;
2835
UNINSTALL SONAME 'file_key_management';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--loose-file-key-management-filename=$MYSQL_TMP_DIR/filekeys-data-too-large.key
1+
--loose-file-key-management-filename=$MYSQLTEST_VARDIR/tmp/filekeys-data-too-large.key
Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
1-
-- source include/not_embedded.inc
1+
--echo #
2+
--echo # MDEV-9158 file_key_management should support reading from a named pipe
3+
--echo #
4+
--source include/not_embedded.inc
5+
--source include/have_sequence.inc
6+
27
--echo # Test checks if opening a too large key file, file key plugin will only read max key file size bytes and extra bytes will be ignored.
38

4-
let $k=0;
5-
while($k<1000){
6-
let $content="";
7-
let $i= 1;
8-
while($i <= 100){
9-
let $num= `SELECT $i+$k*100`;
10-
let $content=$content"$num;24e579b00c0f365a11761bdb9286a72a1148fa779d9c23dd55402627d0c87726";
11-
if($i<=99){
12-
let $content=$content"\n";
13-
}
14-
inc $i;
15-
}
16-
exec echo -e $content >> $MYSQL_TMP_DIR/filekeys-data-too-large.key;
17-
inc $k;
18-
}
19-
--echo #Large key file will read max size bytes , which is 1MB
20-
let SEARCH_PATTERN=read bytes: 1048576B;
21-
call mtr.add_suppression("Syntax error");
22-
call mtr.add_suppression("Invalid key");
9+
replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR;
10+
eval select seq,repeat(md5(seq), 200)
11+
into outfile '$MYSQLTEST_VARDIR/tmp/filekeys-data-too-large.key'
12+
fields terminated by ';'
13+
from seq_1_to_1000;
14+
15+
--echo #Large key file will read max size bytes, which is 1MB
16+
let SEARCH_PATTERN=filekeys-data-too-large.key too large, must be less than 1048576 bytes;
17+
eval call mtr.add_suppression("$SEARCH_PATTERN");
2318
call mtr.add_suppression("Plugin 'file_key_management' init function returned error");
2419
call mtr.add_suppression("Plugin 'file_key_management' registration.*failed");
2520

26-
--disable_result_log
21+
replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR;
2722
--error 2
2823
INSTALL SONAME 'file_key_management';
29-
--enable_result_log
30-
3124

3225
--let SEARCH_FILE= $MYSQLTEST_VARDIR/log/mysqld.1.err
3326
--source include/search_pattern_in_file.inc
27+
28+
--remove_file $MYSQLTEST_VARDIR/tmp/filekeys-data-too-large.key
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--loose-file-key-management-filename=$MYSQL_TMP_DIR/fifo.key
1+
--loose-file-key-management-filename=$MYSQLTEST_VARDIR/tmp/fifo.key

mysql-test/suite/encryption/t/filekeys_named_pipe.test

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1+
--echo #
2+
--echo # MDEV-9158 file_key_management should support reading from a named pipe
3+
--echo #
4+
source include/not_windows.inc;
5+
source include/have_innodb.inc;
6+
source include/have_sequence.inc;
17
#
28
# Test read key from named pipe
39
#
4-
write_file $MYSQL_TMP_DIR/fifo.key;
5-
1;11111111111111111111111111111111
6-
2;00000000000000000000000000000000
7-
3;22222222222222222222222222222222
8-
EOF
9-
--sleep 3
10-
11-
--source include/have_innodb.inc
12-
--replace_result $MYSQL_TMP_DIR MYSQL_TMP_DIR
10+
11+
# large key file, >64K, which is the default value for F_SETPIPE_SZ
12+
--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
13+
eval select seq,md5(seq)
14+
into outfile '$MYSQLTEST_VARDIR/tmp/fifo-key.txt'
15+
fields terminated by ';'
16+
from seq_1_to_20000;
17+
18+
exec mkfifo $MYSQLTEST_VARDIR/tmp/fifo.key;
19+
system cat $MYSQLTEST_VARDIR/tmp/fifo-key.txt > $MYSQLTEST_VARDIR/tmp/fifo.key 2>/dev/null &;
20+
21+
--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
1322
INSTALL SONAME 'file_key_management';
1423

15-
CREATE TABLE t1(c1 BIGINT NOT NULL, b CHAR(200)) ENGINE=INNODB ENCRYPTED=YES ENCRYPTION_KEY_ID=1;
24+
CREATE TABLE t1(c1 BIGINT NOT NULL, b CHAR(200)) ENGINE=INNODB ENCRYPTED=YES ENCRYPTION_KEY_ID=1;
1625
SHOW CREATE TABLE t1;
1726
INSERT t1 VALUES (12345, REPEAT('1234567890', 20));
1827

@@ -23,7 +32,8 @@ SHOW CREATE TABLE t1;
2332

2433
--echo # Reset mysqld
2534
DROP TABLE t1;
26-
remove_file $MYSQL_TMP_DIR/fifo.key;
35+
remove_file $MYSQLTEST_VARDIR/tmp/fifo.key;
36+
remove_file $MYSQLTEST_VARDIR/tmp/fifo-key.txt;
2737
UNINSTALL SONAME 'file_key_management';
2838

2939
--source include/restart_mysqld.inc

plugin/file_key_management/parser.cc

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ bool Parser::read_filekey(const char *filekey, char *secret)
180180
{
181181
my_printf_error(EE_READ,
182182
"Cannot read %s, the filekey is too long, "
183-
"max secret size is %dB ",
183+
"max secret size is %d bytes",
184184
ME_ERROR_LOG, filekey, MAX_SECRET_SIZE);
185185
return 1;
186186
}
@@ -314,7 +314,7 @@ int Parser::parse_line(char **line_ptr, keyentry *key)
314314
char* Parser::read_and_decrypt_file(const char *secret)
315315
{
316316
int f;
317-
ssize_t file_size= 0;
317+
size_t file_size= 0;
318318
if (!filename || !filename[0])
319319
{
320320
my_printf_error(EE_CANT_OPEN_STREAM, "file-key-management-filename is not set",
@@ -330,32 +330,45 @@ char* Parser::read_and_decrypt_file(const char *secret)
330330
}
331331

332332
//Read file into buffer
333-
uchar *buffer;
334-
buffer= (uchar *) malloc((size_t) MAX_KEY_FILE_SIZE );
333+
uchar *buffer, *dst, *end;
334+
buffer= (uchar *) malloc((size_t) MAX_KEY_FILE_SIZE+1 );
335335
if (!buffer)
336336
{
337337
my_error(EE_OUTOFMEMORY, ME_ERROR_LOG | ME_FATAL, file_size);
338338
goto err1;
339339
}
340-
file_size= read(f, buffer, MAX_KEY_FILE_SIZE);
341-
if (file_size < 0)
340+
341+
/* read loop in case fifo buffer size is too small */
342+
dst= buffer;
343+
end= buffer + MAX_KEY_FILE_SIZE + 1;
344+
for (ssize_t len= 1; len && dst < end; dst+= len)
345+
{
346+
if ((len= read(f, dst, (uint)(end - dst))) < 0)
347+
{
348+
my_printf_error(EE_READ, "Read from %s failed, errno %d",
349+
ME_ERROR_LOG, filename, errno);
350+
goto err2;
351+
}
352+
}
353+
file_size= dst - buffer;
354+
if (file_size > MAX_KEY_FILE_SIZE)
342355
{
343-
my_printf_error(EE_READ, "Read from %s failed, errno %d",
344-
ME_ERROR_LOG , filename, errno);
356+
my_printf_error(EE_READ, "File %s too large, must be less than %d bytes",
357+
ME_ERROR_LOG, filename, MAX_KEY_FILE_SIZE);
345358
goto err2;
346359
}
347360
my_printf_error(EE_ERROR_FIRST,
348-
"Read from %s , read bytes: %zdB, max key file size :%dB ",
361+
"Read from %s, read bytes: %zd, max key file size: %d bytes",
349362
ME_ERROR_LOG | ME_NOTE, filename, file_size,
350363
MAX_KEY_FILE_SIZE);
351364
// Check for file encryption
352365
uchar *decrypted;
353-
if ((size_t)file_size > OpenSSL_prefix_len && strncmp((char*)buffer, OpenSSL_prefix, OpenSSL_prefix_len) == 0)
366+
if (file_size > OpenSSL_prefix_len && strncmp((char*)buffer, OpenSSL_prefix, OpenSSL_prefix_len) == 0)
354367
{
355368
uchar key[OpenSSL_key_len];
356369
uchar iv[OpenSSL_iv_len];
357370

358-
decrypted= (uchar*)malloc((size_t)file_size);
371+
decrypted= (uchar*)malloc(file_size+1);
359372
if (!decrypted)
360373
{
361374
my_error(EE_OUTOFMEMORY, ME_ERROR_LOG | ME_FATAL, file_size);

0 commit comments

Comments
 (0)