Skip to content

Fix logic inversion in FlexBuffers VerifyKey() — null terminator check#9072

Open
kakarotsec wants to merge 1 commit intogoogle:masterfrom
kakarotsec:fix-flexbuffers-verifykey
Open

Fix logic inversion in FlexBuffers VerifyKey() — null terminator check#9072
kakarotsec wants to merge 1 commit intogoogle:masterfrom
kakarotsec:fix-flexbuffers-verifykey

Conversation

@kakarotsec
Copy link
Copy Markdown

Problem

VerifyKey() in include/flatbuffers/flexbuffers.h (line 1979) has a logic inversion. It returns true on the first non-zero byte instead of checking for a null terminator:

while (p < buf_ + size_)
  if (*p++) return true;   // returns true on ANY non-zero byte

Since every valid key starts with a non-zero ASCII character, this function effectively always returns true regardless of whether the key has a null terminator within the buffer.

Impact

Buffers with non-null-terminated keys pass VerifyBuffer(). When the application then accesses those keys, strlen() and strcmp() read past the buffer boundary.

VerifyBuffer on CORRUPTED buf: PASS (BUG!)

Key[0]: strlen=15 but key was only "name" (4 chars)
  ^^^ 11 bytes of HEAP MEMORY were read out-of-bounds

map["name"].GetType() = 0 (NULL — lookup FAILED)

ASan output:

==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000013f
READ of size 16 at 0x60200000013f thread T0
    #0 in strlen
0x60200000013f is located 0 bytes after 15-byte region

Fix

-      if (*p++) return true;
+      if (!*p++) return true;

Return true when a zero byte (null terminator) is found. Return false when the end of buffer is reached without finding one.

Verification

After the fix, VerifyBuffer() correctly rejects corrupted buffers:

VerifyBuffer on CORRUPTED buf: FAIL (correct)
Verifier correctly rejected the corrupted buffer. No bug.

@kakarotsec kakarotsec requested a review from dbaileychess as a code owner May 3, 2026 12:37
@github-actions github-actions Bot added the c++ label May 3, 2026
@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 3, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

VerifyKey() returns true on the first non-zero byte instead of
checking for a null terminator. This causes VerifyBuffer() to accept
FlexBuffers with non-null-terminated keys. Subsequent access to those
keys via strlen()/strcmp() reads out of bounds.

The condition if (*p++) should be if (!*p++) — return true
when a null terminator is found, not when any non-zero byte is found.

Confirmed with AddressSanitizer: heap-buffer-overflow in strlen()
after VerifyBuffer() returns true on a corrupted buffer.
@kakarotsec kakarotsec force-pushed the fix-flexbuffers-verifykey branch from 2c64b95 to 76812fd Compare May 3, 2026 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant