Skip to content

Commit

Permalink
MDEV-28315 Fix ASAN stack-buffer-overflow in String::copy_aligned
Browse files Browse the repository at this point in the history
Starting since this commit 36cdd5c
there is an ASAN stack-buffer-overflow error because we append a NULL
terminator beyond the length of memory allocated.

Reviewed by: Monty and Nayuta Yanagisawa
  • Loading branch information
rdtr committed Aug 1, 2022
1 parent 63478e7 commit 84d26f9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
10 changes: 10 additions & 0 deletions mysql-test/main/strings.result
Expand Up @@ -18,3 +18,13 @@ LENGTH(CONCAT_WS(d, ' '))
1
1
DROP TABLE t1;
#
# MDEV-28315 ASAN stack-buffer-overflow in String::copy_aligned
#
CREATE TABLE t1 (a VARBINARY(128)) CHARACTER SET utf32;
INSERT INTO t1 VALUES ('South Carolina, Vermont, New Jersey, New Mexico, Wisconsin, Missouri, Delaware');
CREATE TABLE t2 (b SET('South Carolina', 'Vermont', 'Texas', 'New Mexico', 'Wisconsin', 'Missouri', 'Delaware', 'Wyoming', 'New Jersey', 'Maryland', 'Illinois', 'New York')) CHARACTER SET utf32;
INSERT INTO t2 SELECT * FROM t1;
ERROR 01000: Data truncated for column 'b' at row 1
DROP TABLE t1;
DROP TABLE t2;
12 changes: 12 additions & 0 deletions mysql-test/main/strings.test
Expand Up @@ -24,3 +24,15 @@ CREATE TABLE t1 (d DATE);
INSERT INTO t1 VALUES ('1920-03-02'),('2020-12-01');
SELECT LENGTH(CONCAT_WS(d, ' ')) FROM t1;
DROP TABLE t1;

--echo #
--echo # MDEV-28315 ASAN stack-buffer-overflow in String::copy_aligned
--echo #

CREATE TABLE t1 (a VARBINARY(128)) CHARACTER SET utf32;
INSERT INTO t1 VALUES ('South Carolina, Vermont, New Jersey, New Mexico, Wisconsin, Missouri, Delaware');
CREATE TABLE t2 (b SET('South Carolina', 'Vermont', 'Texas', 'New Mexico', 'Wisconsin', 'Missouri', 'Delaware', 'Wyoming', 'New Jersey', 'Maryland', 'Illinois', 'New York')) CHARACTER SET utf32;
--error WARN_DATA_TRUNCATED
INSERT INTO t2 SELECT * FROM t1;
DROP TABLE t1;
DROP TABLE t2;
2 changes: 1 addition & 1 deletion sql/sql_string.cc
Expand Up @@ -398,7 +398,7 @@ bool String::copy_aligned(const char *str, size_t arg_length, size_t offset,
DBUG_ASSERT(offset && offset != cs->mbminlen);

size_t aligned_length= arg_length + offset;
if (alloc(aligned_length))
if (alloc(aligned_length+1))
return TRUE;

/*
Expand Down
2 changes: 1 addition & 1 deletion sql/sql_string.h
Expand Up @@ -690,7 +690,7 @@ class Binary_string: public Sql_alloc
Note that if arg_length == Alloced_length then we don't allocate.
This ensures we don't do any extra allocations in protocol and String:int,
but the string will not be atomically null terminated if c_ptr() is not
but the string will not be automatically null terminated if c_ptr() is not
called.
*/
if (arg_length <= Alloced_length && Alloced_length)
Expand Down

0 comments on commit 84d26f9

Please sign in to comment.