Skip to content

minor: fix potential soundness hole in fsb dictionary builder - #12397

Merged
Jefffrey merged 1 commit into
apache:mainfrom
Jefffrey:fix-fsb-dict-builder-hole
Nov 23, 2026
Merged

Jefffrey merged 1 commit into
apache:mainfrom
Jefffrey:fix-fsb-dict-builder-hole

Conversation

@Jefffrey

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • N/A

Rationale for this change

Fix a potential soundness issue, see comments for more details

What changes are included in this PR?

Refactoring code to avoid issue.

Are these changes tested?

Existing tests for fsb dictionary builder

Are there any significant user-facing changes?

No

@boring-cyborg boring-cyborg Bot added the arrow-core Core crates related to representing arrow data label Nov 22, 2026
fn get_or_insert_key(&mut self, value: impl AsRef<[u8]>) -> Result<K::Native, ArrowError> {
let value_bytes: &[u8] = value.as_ref();

fn get_or_insert_key(&mut self, value_bytes: &[u8]) -> Result<K::Native, ArrowError> {

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.

the 'hole' is that it could be feasible for an implementation of AsRef<[u8]> to actually return two different values in subsequent calls, in a safe way. so we need to ensure we call it only once, and then pass through &[u8] which is now safe to handle, as passing through impl AsRef<[u8]> gives control over to the implementation

a nice side effect is that i find this to just be cleaner, and we no longer need to make get_or_insert_key generic over impl AsRef<[u8]>

@Rich-T-kid Rich-T-kid 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.

nice find, thanks @Jefffrey

@Rich-T-kid
Rich-T-kid added this pull request to the merge queue Nov 22, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 22, 2026
@Rich-T-kid
Rich-T-kid added this pull request to the merge queue Nov 22, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 23, 2026
@Jefffrey
Jefffrey added this pull request to the merge queue Nov 23, 2026
Merged via the queue into apache:main with commit dcf8262 Nov 23, 2026
22 checks passed
@Jefffrey
Jefffrey deleted the fix-fsb-dict-builder-hole branch November 23, 2026 04:36
Phoenix500526 pushed a commit to Phoenix500526/arrow-rs that referenced this pull request Nov 23, 2026
compiler-segfault added a commit to compiler-segfault/arrow-rs that referenced this pull request Nov 26, 2026
`GenericByteDictionaryBuilder::get_or_insert_key` hashed `value.as_ref()` and
then called `value.as_ref()` again through `storage.append_value(value)`. `AsRef`
is not required to be idempotent, so the stored bytes could differ from the
hashed bytes: the dictionary then holds a value filed under the wrong hash, and
a later append of the value that was actually hashed adds a duplicate entry.

Store the same `&T::Native` that was hashed, the way apache#12397 did it for the
fixed-size binary dictionary builder.

Fixes apache#12424.
compiler-segfault added a commit to compiler-segfault/arrow-rs that referenced this pull request Nov 26, 2026
`GenericByteDictionaryBuilder::get_or_insert_key` hashed `value.as_ref()` and
then called `value.as_ref()` again through `storage.append_value(value)`. `AsRef`
is not required to be idempotent, so the stored bytes could differ from the
hashed bytes: the dictionary then holds a value filed under the wrong hash, and
a later append of the value that was actually hashed adds a duplicate entry.

Store the same `&T::Native` that was hashed, the way apache#12397 did it for the
fixed-size binary dictionary builder.

Fixes apache#12424.
dragonknightk pushed a commit to dragonknightk/arrow-rs that referenced this pull request Nov 26, 2026
Fixes apache#12424.

`GenericByteDictionaryBuilder::get_or_insert_key` hashed
`value.as_ref()` to look the value up, then called `value.as_ref()` a
second time inside the insert closure through
`storage.append_value(value)`. `AsRef` is not required to be idempotent,
so the stored bytes could differ from the bytes that were hashed: the
dictionary then holds a value filed under the wrong hash, and a later
append of the value that was actually hashed adds a duplicate entry.

The builder now stores the same `&T::Native` that it hashed. This is the
same fix apache#12397 made for `FixedSizeBinaryDictionaryBuilder`, which had
the identical pattern.

Verification:

- New test `test_dictionary_builder_hashes_and_stores_the_same_bytes`
uses an `AsRef<str>` that returns a different value on each call; on
`main` it fails with `left: 2, right: 1` (the dictionary ends up with
two entries and keys `[0, 1]` instead of one entry with `[0, 0]`). It
passes with this change.
- `cargo test -p arrow-array`: 767 passed, 1 ignored.
- `cargo fmt --all -- --check` and `cargo clippy -p arrow-array
--all-targets --all-features -- -D warnings` are clean.

The text above was drafted with an AI assistant; I ran the tests myself.

Co-authored-by: compiler-segfault <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-core Core crates related to representing arrow data bug Squash those bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants