Skip to content

conf.c: require the index compare callback in TXT_DB lookups - #12034

Open
yosuke-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f-8864
Open

yosuke-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f-8864

Conversation

@yosuke-wolfssl

Copy link
Copy Markdown
Contributor

Problem

wolfSSL_TXT_DB_create_index() requires a non-NULL cmp callback but discards it, and wolfSSL_TXT_DB_get_by_index() delegates to wolfSSL_lh_retrieve(), which treats any hash match as a hit.

Fix (src/conf.c)

  • WOLFSSL_TXT_DB gains cmp_fn[WOLFSSL_TXT_DB_MAX_FIELDS], appended after hash_fn.
  • wolfSSL_TXT_DB_create_index() stores cmp in cmp_fn[field].
  • wolfSSL_TXT_DB_get_by_index() walks the rows itself and returns a row only when its cached hash matches and cmp_fn(row, value) == 0. It also rejects a NULL value and an index that has no compare callback.

Closes f-8864.

Tests

test_wolfSSL_TXT_DB() re-creates index 3 with a constant hash, so every row collides, and checks:

  • a lookup of 12DA returns the 12DA row (before the fix it returned the first row, 12F8);
  • a lookup of FFFF returns NULL.

Verification

  • The new check fails before the fix and passes after it.
  • ./tests/unit.test (--enable-opensslall --enable-opensslextra): 0 failed, 1461 passed.
  • ASan + UBSan clean on test_wolfSSL_TXT_DB.
  • GCC on Ubuntu 24.04 with -Werror: build clean, test passes.

- WOLFSSL_TXT_DB gains a cmp_fn array.
- wolfSSL_TXT_DB_create_index() stores cmp in cmp_fn[field].
- wolfSSL_TXT_DB_get_by_index() walks the rows itself and returns a
  row only when its hash matches and cmp_fn() returns 0.
- test_wolfSSL_TXT_DB() exercises the colliding hashes case.

Issue: F-8864
@yosuke-wolfssl yosuke-wolfssl self-assigned this Nov 6, 2026
Copilot AI balanced review requested due to automatic review settings November 6, 2026 06:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block approval of the TXT_DB hash-collision fix.

No open issues 🎉

What changed in this PR

This PR fixes hash-collision handling in wolfSSL’s OpenSSL-compatible TXT_DB lookups by requiring both a matching hash and a successful comparison callback.

Changes:

  • Stores a comparison callback for each TXT_DB index.
  • Checks row equality during lookup and rejects NULL values or missing callbacks.
  • Adds collision regression tests for matching and absent values.
File Description
wolfssl/​openssl/​txt_db.h Adds per-index comparison callback storage.
tests/​api.c Tests successful and unsuccessful lookups with colliding hashes.
src/​conf.c Stores comparison callbacks and uses them to verify lookup matches.

Review effort: Balanced

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text +128 B (+0.0%, 800,804 B / 1,048,576 B, total: 76% used)

zephyr-frdm_rw612

  • FLASH: log_const_area -8 B, rodata -8 B, text +1,592 B (+0.6%, 284,040 B / 67,108,864 B, total: 0% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-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.

Fenrir Automated Review — PR #12034

Scan targets checked: wolfssl-src, wolfssl-bugs
Coverage: 3 of 3 in-scope changed file(s) opened by the reviewer

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Max

@yosuke-wolfssl

Copy link
Copy Markdown
Contributor Author

@claude review

@claude claude 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.

I reviewed this PR and didn't find any bugs. It adds a field to the public WOLFSSL_TXT_DB struct in wolfssl/openssl/txt_db.h, which changes the struct layout for OPENSSL_ALL builds, so a maintainer should still confirm that the change is acceptable.

What was reviewed:

  • The new lookup loop in wolfSSL_TXT_DB_get_by_index. When the hash function changes, the existing sync loop resets data->hash to 0, so cached hashes don't go stale when an index is re-created. The new test relies on this when it switches from TXT_DB_hash to TXT_DB_hash_const.
  • wolfssl_sk_node_get_data is static in src/ssl_sk.c. It is still visible here because ssl.c includes ssl_sk.c before conf.c. wolfSSL_TXT_DB_read zeroes the struct with XMEMSET, so cmp_fn starts out NULL.
  • Callers that set hash_fn[idx] directly without calling create_index will now get NULL from every lookup. That only affects code that bypasses the API, so I don't consider it a bug.
Extended reasoning...

The change touches three files. In src/conf.c, the TXT_DB index creation now stores the compare callback, and the lookup now requires both a hash match and a compare match. wolfssl/openssl/txt_db.h gains a public struct field, and tests/api.c adds a test where every hash collides. It doesn't touch crypto, auth or network-parsing paths. The logic is small and follows the existing wolfSSL_lh_retrieve walk, and the test covers it. I deferred for two reasons: the public struct layout (ABI) change needs a maintainer's call, and the earlier reviews from other reviewers have no later commit, so I couldn't confirm they were addressed.

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.

3 participants