Skip to content

bound the trailing hex read in my_mb_wc_filename#5187

Open
jmestwa-coder wants to merge 1 commit into
MariaDB:mainfrom
jmestwa-coder:filename-mbwc-overread
Open

bound the trailing hex read in my_mb_wc_filename#5187
jmestwa-coder wants to merge 1 commit into
MariaDB:mainfrom
jmestwa-coder:filename-mbwc-overread

Conversation

@jmestwa-coder

Copy link
Copy Markdown
Contributor

my_mb_wc_filename decodes the 5-byte '@hhhh' escape but the length guard is off by one:

  • the s + 4 > e check only covers 4 bytes while the hex branch reads s[4]
  • a truncated @HHH whose buffer ends at s+4 over-reads one byte past e, reachable through well_formed_length/charpos on a my_charset_filename string with tight bounds
  • the existing s[3] ? guard only stops at a NUL terminator, not at the end pointer
    bound the s[4] read against e; added a strings-t case that returns ILSEQ instead of over-reading (ASAN flags the read of size 1 past a 4-byte buffer before the fix).

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes an out-of-bounds read vulnerability in my_mb_wc_filename by ensuring that the end pointer is checked before reading the 4th hex digit of a truncated escape sequence. It also adds a unit test to verify this behavior. The review comments suggest improving the unit test by returning the actual return value of my_ci_mb_wc and asserting the expected error code (MY_CS_ILSEQ) directly, which makes the test more idiomatic and direct.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +1714 to +1721
static int test_mb_wc_filename_truncated()
{
CHARSET_INFO *cs= &my_charset_filename;
uchar buf[5]= {'@', '0', '0', '0', '1'};
my_wc_t wc= 0;
int rc= my_ci_mb_wc(cs, &wc, buf, buf + 4);
return rc != MY_CS_ILSEQ;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Returning the actual return value of my_ci_mb_wc rather than a boolean mapping makes the test more direct and easier to understand. We can then assert the expected error code (MY_CS_ILSEQ) directly in the test runner.

static int test_mb_wc_filename_truncated()
{
  CHARSET_INFO *cs= &my_charset_filename;
  uchar buf[5]= {'@', '0', '0', '0', '1'};
  my_wc_t wc= 0;
  return my_ci_mb_wc(cs, &wc, buf, buf + 4);
}

Comment thread unittest/strings/strings-t.c Outdated
Comment on lines +1757 to +1758
ok(test_mb_wc_filename_truncated() == 0,
"filename decoder does not read past the end pointer");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Asserting the expected error code (MY_CS_ILSEQ) directly is more idiomatic and provides clearer test output if the assertion fails.

  ok(test_mb_wc_filename_truncated() == MY_CS_ILSEQ,
     "filename decoder does not read past the end pointer");

@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Jun 8, 2026

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! This is a preliminary review.

Thank you for working out a test this time!
FWIW, the whole thing is highly theoretical IMHO, since right now most of the file name buffers are of fixed size. I'd be VERY surprised if you find an integral test with this. Have you thought about a possible one?

Otherwise, on the formal side, please add a commit message to your commit.

Comment thread strings/ctype-utf8.c Outdated
{
int byte3= hexlo(s[3]);
int byte4= hexlo(s[3] ? s[4] : 0);
int byte4= hexlo((s[3] && s + 4 < e) ? s[4] : 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, @NNN (3 symbols) is not a valid escape! You could just change the check above to say s + 5 and be done with it (and remove all the ifs and buts).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, @hhh isn't a valid escape. Switched the check above to s + 5 > e (TOOSMALL5) and dropped the per-byte guards, so s[3]/s[4] are plain hexlo() reads now.

@gkodinov gkodinov self-assigned this Jun 8, 2026
The my_charset_filename '@hhhh' escape is 5 bytes, but the length guard
only required 4 (s[0..3]) before the hex branch read s[4]. A truncated
'@hhh' whose buffer ends exactly at s+4 therefore read one byte past the
end pointer, reachable through well_formed_length/charpos on a string
with tight bounds.

A 4-byte escape is not valid anyway, so require all 5 bytes up front and
return MY_CS_TOOSMALL5 when they are not there. That makes the per-byte
end-pointer checks on s[3]/s[4] unnecessary, so drop them.

Adds a strings-t case that feeds a truncated escape with end= buf+4 and
checks for MY_CS_TOOSMALL5; before the fix it read the byte past the end
pointer (ASAN: heap-buffer-overflow read of size 1).
@jmestwa-coder jmestwa-coder force-pushed the filename-mbwc-overread branch from a62f68b to b1bf9b3 Compare June 8, 2026 17:23
@jmestwa-coder

Copy link
Copy Markdown
Contributor Author

Pushed both points:

  • guard is now s + 5 > e and the per-byte ifs are gone, per your note
  • added a commit message body

On a real-world trigger: agreed, it's theoretical. The filename buffers on the server paths are all fixed-size and NUL-terminated, so I couldn't get a SQL-level case to reach s[4] past the end either. The only route that does is well_formed_length/charpos with a tight, non-terminated end pointer, which is what the strings-t case feeds. I don't have an integral test to offer beyond that one, so happy to drop it if you'd rather not carry the unit test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

2 participants