Skip to content

Conversation

@glennsong09
Copy link
Collaborator

@glennsong09 glennsong09 commented Oct 28, 2025

Important

Add size field to H5D_compact_storage_t and update related functions to prevent buffer overflows.

  • Behavior:
    • Add size field to H5D_compact_storage_t to track buffer size.
    • Update H5D__compact_readvv() in H5Dcompact.c to check buffer size before reading.
    • Modify H5D__chunk_lock() in H5Dchunk.c to initialize and set alloc_chunk_size.
  • Functions:
    • Update H5D__chunk_lock() to accept size_t *alloc_chunk_size and set it during chunk allocation.
    • Modify H5D__chunk_read() and H5D__chunk_write() in H5Dchunk.c to use alloc_chunk_size.
  • Structures:
    • Add size field to H5D_compact_storage_t in H5Dpkg.h to store buffer size.

This description was created by Ellipsis for 72bdcc4. You can customize this summary. It will automatically update as commits are pushed.

@glennsong09 glennsong09 marked this pull request as draft October 28, 2025 20:39
@nbagha1 nbagha1 added this to the Release 2.0.0 milestone Oct 29, 2025

/* Check if src buffer size is less than expected */
if (src_alloc_size > 0) {
if (src_alloc_size < tmp_src_len)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just checking tmp_src_len isn't enough here, as there may be more than one sequence and depending on which branch is taken below, either tmp_src_len or tmp_dst_len will need to be checked. You can see that tmp_src_len and tmp_dst_len are continually updated in the loops below, so you'll need to check tmp_src_len/tmp_dst_len against the remaining space left in _src on each iteration, which will involve keeping track of the end of _src and using that alongside the current offset into it (src). The checking can probably be done in the usual way with the H5_IS_BUFFER_OVERFLOW macro.

/* Check if src buffer size is less than expected */
if (src_alloc_size > 0) {
if (src_alloc_size < tmp_src_len)
HGOTO_ERROR(H5E_INTERNAL, H5E_CANTOPERATE, FAIL, "src buffer size is less than expected");
Copy link
Collaborator

Choose a reason for hiding this comment

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

The values are the same, but since this function returns an ssize_t currently, (-1) should be explicitly returned for consistency.

/* Perform vectorized memcpy from src_buf to dst_buf */
if ((bytes_copied = H5VM_memcpyvv(dst_buf, dst_nseq, &curr_dst_seq, dst_len, dst_off, src_buf,
src_nseq, &curr_src_seq, src_len, src_off)) < 0)
src_nseq, &curr_src_seq, src_len, src_off, 0)) < 0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd maybe consider SIZE_MAX instead of 0 for an unknown value, but in this case we should probably make sure we always have a known value to pass to H5VM_memcpyvv.

static void *
H5D__chunk_lock(const H5D_io_info_t H5_ATTR_NDEBUG_UNUSED *io_info, const H5D_dset_io_info_t *dset_info,
H5D_chunk_ud_t *udata, bool relax, bool prev_unfilt_chunk)
H5D_chunk_ud_t *udata, bool relax, bool prev_unfilt_chunk, size_t *alloc_chunk_size)
Copy link
Collaborator

Choose a reason for hiding this comment

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

In this function, it looks like there are several places where the final value for alloc_chunk_size should be set. At least:

  • After the chunk entry is retrieved from the cache on line 4372 (which means the H5D_rdcc_ent_t structure is probably also going to need to track the size of the allocated chunk buffer)
  • After the call to H5D__chunk_mem_alloc() on line 4408
  • After the call to H5D__chunk_mem_alloc() on line 4434
  • After the call to H5D__chunk_mem_alloc() on line 4515
  • After the call to H5D__chunk_mem_alloc() on line 4535 (as is done currently in this PR)
  • After the call to H5Z_pipeline() on line 4553 (as is done currently in this PR, but using the value of buf_alloc that comes back rather than setting alloc_chunk_size to 0)
  • After the call to H5D__chunk_mem_alloc() on line 4561
  • After the call to H5D__chunk_mem_alloc() on line 4582

Due to all these places, it would probably be easiest to keep track of the value in a local variable and assign the value to alloc_chunk_size somewhere toward the end of the function.

@ajelenak ajelenak removed this from the Release 2.0.0 milestone Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To be triaged

Development

Successfully merging this pull request may close these issues.

4 participants