Skip to content

Read commit-graph, midx and pack index integers without pointer casts - #7441

Open
vera-rubin wants to merge 2 commits into
libgit2:mainfrom
vera-rubin:fix/read-be-without-casts
Open

vera-rubin wants to merge 2 commits into
libgit2:mainfrom
vera-rubin:fix/read-be-without-casts

Conversation

@vera-rubin

Copy link
Copy Markdown

These readers read on-disk integers with ntohl(*(uint32_t *)p) and keep the fanout tables as const uint32_t * into file data. That assumes 4-byte alignment, which nothing checks; the fuzzers skip four bytes to stay aligned.

This adds git__get_be32()/git__get_be64() (byte composition, like git's get_be32()), uses them throughout those readers, and copies file headers into a local struct as index.c does.

I left bloom_filter_indexes alone since #7357 builds on it.

If it lands, we may submit it to Google's Patch Rewards program, and we'd be glad to share any reward with the maintainers.

— ChipLab

Add helpers that read a big-endian integer from a byte buffer by
composing it from its bytes, as git's own get_be32() and get_be64()
do. Unlike `ntohl(*(uint32_t *)p)`, they make no assumption about the
alignment of `p`. Compilers recognize the pattern and emit a single
load and byte swap where the platform allows unaligned loads.
The commit-graph, multi-pack-index and pack index readers read 32-bit
fields by casting a pointer into the file data to `uint32_t *` (or to
a header struct) and dereferencing it, and keep the fanout tables as
`const uint32_t *`. That is only valid when the data is suitably
aligned. The parsers check chunk offsets against the file size but not
their alignment, and git_commit_graph_file_parse() and git_midx_parse()
take any buffer; the fuzzers skip four input bytes just to keep theirs
aligned.

Read these fields with git__get_be32() and git__get_be64() instead,
keep the fanout tables as byte pointers, and copy the file headers into
a local struct, as index.c already does for index entries. The parsed
values are unchanged.

Add tests that parse a commit-graph and a multi-pack-index from an odd
address. Without this change they fail under the UBSan CI job.

This branch has not been deployed

No deployments
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.

1 participant