MDEV-40385 use-of-uninitialized-value in Binary_string::c_ptr()#5448
Open
LukeYL026 wants to merge 2 commits into
Open
MDEV-40385 use-of-uninitialized-value in Binary_string::c_ptr()#5448LukeYL026 wants to merge 2 commits into
LukeYL026 wants to merge 2 commits into
Conversation
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
LukeYL026
force-pushed
the
mdev-40385-cptr-use-of-uninitialized-value
branch
from
July 23, 2026 18:25
1a3f8b8 to
17d6a3a
Compare
gkodinov
requested changes
Jul 24, 2026
gkodinov
left a comment
Member
There was a problem hiding this comment.
Thank you for your contribution! This is a preliminary review.
SELECT KDF('','',1000,256) triggered an MSAN use-of-uninitialized-value
report in Binary_string::c_ptr() (sql/sql_string.h) reached from
Item_func_kdf::val_str().
The optional 4th argument (kdf_name) was evaluated into a result buffer
and then read as a C string with c_ptr(). When that argument is an
integer literal such as 256, Item_int::val_str() writes the digits "256"
into the buffer without appending a trailing NUL, and String::alloc()
intentionally skips reallocation, so the buffer stays non-"alloced" and
unterminated. c_ptr() then reads Ptr[str_length] to test for an existing
terminator, reading an uninitialized byte. The buffer is a stack-resident
ValueBuffer from Protocol::send_result_set_row, so the byte is validly
addressable but never initialized; only MSAN re-poisons the stack scope,
which is why the report is MSAN-only and Valgrind does not flag it.
Use c_ptr_safe() instead of c_ptr() when reading the kdf_name argument.
c_ptr_safe() writes the NUL terminator after a capacity check without
first reading Ptr[str_length], while c_ptr() reads that byte to detect an
existing terminator. Behaviour is unchanged: a non-matching kdf_name still
yields ER_STD_INVALID_ARGUMENT and NULL, and valid names (pbkdf2_hmac,
hkdf) still work.
The regression test main.mdev40385 reproduces the report only under an
MSAN-instrumented build; on a normal build it passes on both the unfixed
and fixed server because the stray read is harmless without a sanitizer.
All new code of the whole pull request, including one or several files that
are either new files or modified ones, are contributed under the BSD-new
license. I am contributing on behalf of my employer Amazon Web Services,
Inc.
LukeYL026
force-pushed
the
mdev-40385-cptr-use-of-uninitialized-value
branch
from
July 24, 2026 18:19
17d6a3a to
c08441e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
SELECT KDF('','',1000,256)triggered an MSAN use-of-uninitialized-valuereport originating in
Binary_string::c_ptr()(sql/sql_string.h) reached fromItem_func_kdf::val_str()(sql/item_strfunc.cc).In
Item_func_kdf::val_str(String *buf)the optional 4th argument (kdf_name)was evaluated into the shared result buffer
bufand then read as a Cstring:
The issue is when
args[3]is an integer literal (here256),Item_int::val_str()writes the digits"256"(length 3) into the buffer, andString::alloc()intentionally skips reallocation when the buffer is already large enough — so the buffer stays non-allocedand is left without a trailing NUL.c_ptr()then executes:Because the buffer is not
alloced,c_ptr()first readsPtr[str_length](i.e.
buf[3]) to decide whether a terminator already exists. That byte isvalidly addressable (the buffer is large) but was never initialized, so MSAN
flags a use-of-uninitialized-value. Valgrind does not flag it because that stack
byte was typically written by an earlier call frame; only MSAN re-poisons the
stack scope, which is why the report is MSAN-only.
Fix
sql/item_strfunc.cc,Item_func_kdf::val_str():s->c_ptr_safe()instead ofs->c_ptr().c_ptr_safe()never readsPtr[str_length]; it writes the terminator (or reallocates), removing theuninitialized read regardless of the buffer's
allocedstate.Behaviour is unchanged for callers: a non-matching
kdf_namestill yields theER_STD_INVALID_ARGUMENTwarning andNULL, and valid names (pbkdf2_hmac,hkdf) still work.Release Notes
Resolved uninitialized read deficiency in
Binary_string::c_ptr()viaitem_func_kdf::val_str().How can this PR be tested?
Because this stray read is harmless without a sanitizer, related MTR tests need to be run on an MSAN build to observe regression. Run MTR on an MSAN build by adding build flag
-DWITH_MSAN=ON(clang), which requires MSAN-instrumented systemlibraries — see
https://mariadb.com/kb/en/compile-and-using-mariadb-with-sanitizers-asan-ubsan-tsan-msan/
The following test and result files were added as regression tests:
mysql-test/main/mdev40385.testmysql-test/main/mdev40385.resultBasing the PR against the correct MariaDB version
Based against
11.4, the oldest maintained LTS affected. The JIRA MSAN matrixshows
10.6and10.11as unaffected because theKDF()function does notexist there; the defect is present from
11.4onward.Copyright
All new code of the whole pull request, including one or several files that are
either new files or modified ones, are contributed under the BSD-new license. I
am contributing on behalf of my employer Amazon Web Services, Inc.