fix(h5diff): heap buffer over-read when comparing fixed-length null-terminated strings
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 76/100
Research direction
Start in tools/lib/h5diff_array.c at diff_datum(), where the issue identifies the unbounded strlen() calls for s1 and s2. Check nearby tests for fixed-length H5T_STR_NULLTERM values that fill their buffers, then run the relevant h5diff and h5repack tests. Done means both strings are measured without reading beyond size_mtype and the regression tests pass.
Written by the indexing model from the issue text.
Description
Describe the bug
diff_datum() in tools/lib/h5diff_array.c computes the length of fixed-length, null-terminated (H5T_STR_NULLTERM) strings with strlen() and only afterwards clamps the result to the string size:
s1 = (char *)mem1;
if (s1)
size1 = strlen(s1);
...
if (size1 > size_mtype)
size1 = size_mtype;
A string that fills its whole buffer has no terminator inside it, so strlen() reads past the end of the buffer before the clamp is applied. The same pattern is used for s2.
On most platforms the over-read usually lands in readable memory and the clamp hides it. On OpenBSD, whose malloc places guard pages after allocations, it crashes:
H5REPACK_VFD-stdio-h5repacktest ***Failed (Segmentation fault)
H5REPACK_VFD-split-h5repacktest ***Failed (Segmentation fault)
H5REPACK_VFD-family-h5repacktest ***Failed (Segmentation fault)
Backtrace from the core file:
frame #0: libc.so.104.0`strlen
frame #1: libhdf5_tools.so`diff_datum
frame #2: libhdf5_tools.so`diff_array
frame #3: libhdf5_tools.so`diff_attr_data
frame #4: libhdf5_tools.so`diff_attr
frame #5: libhdf5_tools.so`diff
frame #6: libhdf5_tools.so`diff_match
frame #7: libhdf5_tools.so`h5diff
frame #8: h5repacktest`main
The crash happens in the "copy of datasets (attributes)" step for stdio and split, and in "latest file format options" for family.
Expected behavior
h5diff never reads beyond the fixed-length string buffer.
Platform
- HDF5 develop
- OpenBSD 8.0 amd64, clang, Release build,
-DHDF5_TEST_VFD=ON - On Linux the tests pass, but the out-of-bounds read is the same on every platform and can crash wherever the buffer ends at an unmapped page (hardened allocators, large mmap-backed allocations, sanitizers).
Suggested fix
Use strnlen(s, size_mtype), which gives the same length as strlen() followed by the clamp, without reading past the buffer.
- Dominant language
- C
- Stars
- 1k
- Forks
- 375
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 51
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from HDFGroup/hdf5
-
Fix a typo in H5Centry.cPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
GCC 15.2: -Wcast-qual warning - cast discards const qualifier from pointer target type (H5trace.c)Possibly taken @jhendersonHDF claimed this 11 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
HDFGroup/hdf5#6878 · 1 assignee ·
Maintainers usually reply within 1 day
-
GCC 15.2: -Wformat-y2k warning in H5_get_localtime_str ('%c' yields only last 2 digits of year in some locales)Possibly taken @fortnern claimed this 11 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 82/100
HDFGroup/hdf5#6876 · 1 assignee ·
Maintainers usually reply within 1 day
-
H5EA header deserialize computes index block statistics from nsblks before H5EA__hdr_init sets itPossibly taken @vchoi-hdfgroup claimed this 14 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
HDFGroup/hdf5#6869 · 1 assignee ·
Maintainers usually reply within 1 day
-
hdf5_hl_fortran.pc doesn't require hdf5_hl, so static high-level Fortran links failPossibly taken @jhendersonHDF claimed this 25 days ago. OpenComponent - Build
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
HDFGroup/hdf5#6819 · 1 assignee ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
new issue
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
llvm/llvm-project#245835 ·
Maintainers usually reply within 1 day
-
needs-triage
Difficulty 1/5 Under an hour Newbie friendliness 72/100
InauguralSystems/EigenScript#2537 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
taosdata/TDengine#35812 · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
openwrt/openwrt#26942 · 1 comment ·
Maintainers usually reply within 1 day