Skip to content

Commit 873cc1e

Browse files
committed
MDEV-21839: Handle crazy offset to SHOW BINLOG EVENTS
Problem: ======= SHOW BINLOG EVENTS FROM <"random"-pos> caused a variety of failures as reported in MDEV-18046. They are fixed but that approach is not future-proof as well as is not optimal to create extra check for being constructed event parameters. Analysis: ========= "show binlog events from <pos>" code considers the user given position as a valid event start position. The code starts reading data from this event start position onwards and tries to map it to a set of known events. Each event has a specific event structure and asserts have been added to ensure that, read event data, satisfies the event specific requirements. When a random position is supplied to "show binlog events command" the event structure specific checks will fail and they result in assert. For example: https://jira.mariadb.org/browse/MDEV-18046 In the bug description user executes CREATE TABLE/INSERT and ALTER SQL commands. When a crazy offset like "SHOW BINLOG EVENTS FROM 365" is provided code assumes offset 365 as valid event begin and proceeds to EVENT_LEN_OFFSET reads some random length and comes up with a crazy event which didn't exits in the binary log. In this quoted example scenario, event read at offset 365 is considered as "Update_rows_log_event", which is not present in binary log. Since this is a random event its validation fails and code results in assert/segmentation fault, as shown below. mysqld: /data/src/10.4/sql/log_event.cc:10863: Rows_log_event::Rows_log_event( const char*, uint, const Format_description_log_event*): Assertion `var_header_len >= 2' failed. 181220 15:27:02 [ERROR] mysqld got signal 6 ; #7 0x00007fa0d96abee2 in __assert_fail () from /lib/x86_64-linux-gnu/libc.so.6 #8 0x000055e744ef82de in Rows_log_event::Rows_log_event (this=0x7fa05800d390, buf=0x7fa05800d080 "", event_len=254, description_event=0x7fa058006d60) at /data/src/10.4/sql/log_event.cc:10863 #9 0x000055e744f00cf8 in Update_rows_log_event::Update_rows_log_event Since we are reading random data repeating the same command SHOW BINLOG EVENTS FROM 365 produces different types of crashes with different events. MDEV-18046 reported 10 such crashes. In order to avoid such scenarios user provided starting offset needs to be validated for its correctness. Best way of doing this is to make use of checksums if they are available. MDEV-18046 fix introduced the checksum based validation. The issue still remains in cases where binlog checksums are disabled. Please find the following bug reports. MDEV-22473: binlog.binlog_show_binlog_event_random_pos failed in buildbot, server crashed in read_log_event MDEV-22455: Server crashes in Table_map_log_event, binlog.binlog_invalid_read_in_rotate failed in buildbot Fix: ==== When binlog checksum is disabled, perform scan(via reading event by event), to validate the requested FROM <pos> offset. Starting from offset 4 read the event_length of next_event in the binary log. Using the next_event length advance current offset to point to next event. Repeat this process till the current offset is less than or equal to crazy offset. If current offset is higher than crazy offset provide appropriate invalid input offset error.
1 parent 5768f57 commit 873cc1e

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

mysql-test/r/ctype_cp932_binlog_stm.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ master-bin.000001 # Query # # use `test`; DROP TABLE `t4` /* generated by server
5656
End of 5.0 tests
5757
call mtr.add_suppression("Error in Log_event::read_log_event\\\(\\\): 'Found invalid");
5858
SHOW BINLOG EVENTS FROM 504;
59-
ERROR HY000: Error when executing command SHOW BINLOG EVENTS: Wrong offset or I/O error
59+
ERROR HY000: Error when executing command SHOW BINLOG EVENTS: Invalid input pos specified please provide valid one.
6060
Bug#44352 UPPER/LOWER function doesn't work correctly on cp932 and sjis environment.
6161
CREATE TABLE t1 (a varchar(16)) character set cp932;
6262
INSERT INTO t1 VALUES (0x8372835E),(0x8352835E);

mysql-test/suite/binlog/r/binlog_show_binlog_event_random_pos.result

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ INSERT INTO t1 VALUES (repeat('a', 255), repeat('a', 255),repeat('a', 255),repea
99
INSERT INTO t1 VALUES (repeat('a', 255), repeat('a', 255),repeat('a', 255),repeat('a', 255),repeat('a', 255));
1010
UPDATE t1 SET c1=repeat('b',255);
1111
INSERT INTO t1 VALUES (repeat('a', 255), repeat('a', 255),repeat('a', 255),repeat('a', 255),repeat('a', 255));
12+
SHOW BINLOG EVENTS FROM POS;
13+
ERROR HY000: Error when executing command SHOW BINLOG EVENTS: Invalid pos specified. Requested from pos:POS is greater than actual file size:MAX_POS
14+
1215
DROP TABLE t1;

mysql-test/suite/binlog/t/binlog_show_binlog_event_random_pos.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ while ($pos <= $max_pos)
3434
--enable_query_log
3535
}
3636

37+
# Testing a case where input position is greater than actual binlog file size.
38+
--replace_result $pos POS $max_pos MAX_POS
39+
--error 1220
40+
eval SHOW BINLOG EVENTS FROM $pos;
41+
3742
DROP TABLE t1;

sql/sql_repl.cc

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3842,6 +3842,7 @@ bool mysql_show_binlog_events(THD* thd)
38423842
{
38433843
Protocol *protocol= thd->protocol;
38443844
List<Item> field_list;
3845+
char errmsg_buf[MYSYS_ERRMSG_SIZE];
38453846
const char *errmsg = 0;
38463847
bool ret = TRUE;
38473848
/*
@@ -3856,6 +3857,9 @@ bool mysql_show_binlog_events(THD* thd)
38563857
Master_info *mi= 0;
38573858
LOG_INFO linfo;
38583859
LEX_MASTER_INFO *lex_mi= &thd->lex->mi;
3860+
enum enum_binlog_checksum_alg checksum_alg;
3861+
my_off_t binlog_size;
3862+
MY_STAT s;
38593863

38603864
DBUG_ENTER("mysql_show_binlog_events");
38613865

@@ -3904,10 +3908,6 @@ bool mysql_show_binlog_events(THD* thd)
39043908
mi= 0;
39053909
}
39063910

3907-
/* Validate user given position using checksum */
3908-
if (lex_mi->pos == pos && !opt_master_verify_checksum)
3909-
verify_checksum_once= true;
3910-
39113911
unit->set_limit(thd->lex->current_select);
39123912
limit_start= unit->offset_limit_cnt;
39133913
limit_end= unit->select_limit_cnt;
@@ -3934,6 +3934,17 @@ bool mysql_show_binlog_events(THD* thd)
39343934
if ((file=open_binlog(&log, linfo.log_file_name, &errmsg)) < 0)
39353935
goto err;
39363936

3937+
my_stat(linfo.log_file_name, &s, MYF(0));
3938+
binlog_size= s.st_size;
3939+
if (lex_mi->pos > binlog_size)
3940+
{
3941+
sprintf(errmsg_buf, "Invalid pos specified. Requested from pos:%llu is "
3942+
"greater than actual file size:%lu\n", lex_mi->pos,
3943+
(ulong)s.st_size);
3944+
errmsg= errmsg_buf;
3945+
goto err;
3946+
}
3947+
39373948
/*
39383949
to account binlog event header size
39393950
*/
@@ -3985,7 +3996,43 @@ bool mysql_show_binlog_events(THD* thd)
39853996
}
39863997
}
39873998

3988-
my_b_seek(&log, pos);
3999+
if (lex_mi->pos > BIN_LOG_HEADER_SIZE)
4000+
{
4001+
checksum_alg= description_event->checksum_alg;
4002+
/* Validate user given position using checksum */
4003+
if (checksum_alg != BINLOG_CHECKSUM_ALG_OFF &&
4004+
checksum_alg != BINLOG_CHECKSUM_ALG_UNDEF)
4005+
{
4006+
if (!opt_master_verify_checksum)
4007+
verify_checksum_once= true;
4008+
my_b_seek(&log, pos);
4009+
}
4010+
else
4011+
{
4012+
my_off_t cur_pos= my_b_tell(&log);
4013+
ulong next_event_len= 0;
4014+
uchar buff[IO_SIZE];
4015+
while (cur_pos < pos)
4016+
{
4017+
my_b_seek(&log, cur_pos + EVENT_LEN_OFFSET);
4018+
if (my_b_read(&log, (uchar *)buff, sizeof(next_event_len)))
4019+
{
4020+
mysql_mutex_unlock(log_lock);
4021+
errmsg = "Could not read event_length";
4022+
goto err;
4023+
}
4024+
next_event_len= uint4korr(buff);
4025+
cur_pos= cur_pos + next_event_len;
4026+
}
4027+
if (cur_pos > pos)
4028+
{
4029+
mysql_mutex_unlock(log_lock);
4030+
errmsg= "Invalid input pos specified please provide valid one.";
4031+
goto err;
4032+
}
4033+
my_b_seek(&log, cur_pos);
4034+
}
4035+
}
39894036

39904037
for (event_count = 0;
39914038
(ev = Log_event::read_log_event(&log, (mysql_mutex_t*) 0,

0 commit comments

Comments
 (0)