Fix mstrGetFlag out-of-bounds read without metadata by Huang-404-Q · Pull Request #16023 · redis/redis · GitHub
Skip to content

Fix mstrGetFlag out-of-bounds read without metadata - #16023

Open
Huang-404-Q wants to merge 1 commit into
redis:unstablefrom
Huang-404-Q:fix/mstr-getflag-no-meta
Open

Huang-404-Q wants to merge 1 commit into
redis:unstablefrom
Huang-404-Q:fix/mstr-getflag-no-meta

Conversation

@Huang-404-Q

@Huang-404-Q Huang-404-Q commented Oct 21, 2026 •

Copy link
Copy Markdown

Summary

mstrGetFlag() called mstrFlagsRef() unconditionally, which points before the allocation when no metadata is attached. Reading the flags word then reads 2 bytes to the left of the allocation (ASAN heap-buffer-overflow), and a caller that trusts a nonzero result can reach an out-of-range metadata pointer.

Check mstrIsMetaAttached() first, as the header comment already tells callers to do.

Test

Adds an mstrTest case for a metadata-less string.

make -j4 MALLOC=libc SANITIZER=address REDIS_CFLAGS='-DREDIS_TEST'
./src/redis-server test mstr

AI assistance

Tool(s) used: Claude Code

How you used it: the assistant read the code, wrote the fix and the test, and ran the built-in test. A human reviewed the diff and takes responsibility for the change.

  • I've read and understand every line of this change and take responsibility for it — it's not unreviewed AI output.

Note

Low Risk
Small, localized safety fix in an inline helper plus a unit test; behavior for metadata-backed strings is unchanged.

Overview
Fixes mstrGetFlag so it no longer reads flag bits when an mstr has no metadata attached. The inline helper now returns 0 unless mstrIsMetaAttached(s) is true, matching the documented expectation that callers should verify metadata before using flag/metadata APIs.

Previously it always called mstrFlagsRef() and dereferenced the flags word; without metadata that address sits before the allocation, which triggers ASAN heap-buffer-overflow and could yield bogus nonzero flags.

Adds an mstrTest case that builds a plain mstrNew string and asserts mstrGetFlag(s, 0) == 0.

Reviewed by Cursor Bugbot for commit e61d1a8. Bugbot is set up for automated code reviews on this repo. Configure here.

mstrGetFlag() called mstrFlagsRef() unconditionally, which points before
the allocation when no metadata is attached. Reading the flags word then
reads 2 bytes to the left of the allocation (ASAN heap-buffer-overflow),
and a caller that trusts a nonzero result can reach an out-of-range
metadata pointer.

Check mstrIsMetaAttached() first, as the header comment already tells
callers to do.

Adds an mstrTest case for a metadata-less string.
@CLAassistant

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants