Fix out-of-bounds read / size_t underflow in dkimf_db_datasplit() - #425
Merged
thegushi merged 1 commit intoJul 22, 2026
Merged
Conversation
In the colon-delimited-field branch, dkimf_db_datasplit() located the next delimiter with strchr(p, ':'), which scans until it finds ':' or a NUL byte, ignoring the "remain" byte budget entirely. For the Berkeley DB backend this happened to be harmless: the destination buffer is memset to all-zero for its full length before a DB_DBT_USERMEM fetch that only ever writes up to buflen bytes, so there's always a guaranteed trailing NUL just past the data. The LMDB backend has no such guarantee: mdb_get()/mdb_cursor_get() return a pointer directly into LMDB's memory-mapped pages with no NUL-termination promise. If a stored value has no ':' within its real length, strchr() reads past the value into adjacent mapped memory, "clen = q - p" can exceed "remain", and "remain -= (clen + 1)" underflows the size_t to a huge value -- the loop then keeps running with a corrupted, out-of-bounds pointer. This affects any DataSet/SigningTable-style DB backed by LMDB that uses colon-delimited multi-field values. Fixed by using memchr(p, ':', remain) instead, which respects the "remain" window by construction and can never return a pointer past p + remain, so the subtraction can no longer underflow.
2 tasks
thegushi
added a commit
that referenced
this pull request
Jul 22, 2026
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.
Summary
dkimf_db_datasplit()(opendkim/opendkim-db.c) splits a colon-delimited DB value into fields. Its delimiter search usedstrchr(p, ':'), which scans until it finds':'or a NUL byte -- ignoring theremainbyte budget it's supposed to respect.For the Berkeley DB backend this happens to be harmless: the destination buffer is
memsetto all-zero for its full length before aDB_DBT_USERMEMfetch that only ever writes up tobuflenbytes, so there's always a guaranteed trailing NUL just past the real data.The LMDB backend has no such guarantee:
mdb_get()/mdb_cursor_get()return a pointer directly into LMDB's memory-mapped pages, with no NUL-termination promise. If a stored value has no':'within its real length,strchr()reads past the value into adjacent mapped memory,clen = q - pcan exceedremain, andremain -= (clen + 1)underflows thesize_tto a huge value -- the loop then keeps running with a corrupted, out-of-bounds pointer. This affects any DataSet/SigningTable-style DB backed by LMDB that uses colon-delimited multi-field values (reqnum > 1, non-binary fields).I traced every caller of
dkimf_db_datasplit()(Berkeley DB, LMDBdkimf_db_get(), and the LMDB cursor-walk path) to confirm the Berkeley DB call sites are safe by accident and the LMDB ones are not.Changes
strchr(p, ':')withmemchr(p, ':', remain), which respects theremainwindow by construction and can never return a pointer pastp + remain, so the subsequent subtraction can no longer underflow.Test plan
autoreconf -fi && ./configure && gmakebuilds cleangmake check: 174/174 tests pass