Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes after merge of file format security fixes #4263

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/H5Dint.c
Original file line number Diff line number Diff line change
Expand Up @@ -947,8 +947,8 @@ H5D__update_oh_info(H5F_t *file, H5D_t *dset, hid_t dapl_id)
HGOTO_ERROR(H5E_DATASET, H5E_CANTPIN, FAIL, "unable to pin dataset object header");

/* Check for creating dataset with unusual datatype */
if (H5T_is_numeric_with_unusual_unused_bits(type) &&
!(H5O_has_chksum(oh) || (H5F_RFIC_FLAGS(file) & H5F_RFIC_UNUSUAL_NUM_UNUSED_NUMERIC_BITS)))
if (!(H5O_has_chksum(oh) || (H5F_RFIC_FLAGS(file) & H5F_RFIC_UNUSUAL_NUM_UNUSED_NUMERIC_BITS)) &&
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Per @fortnern's comment, invert a couple of these checks to do the easy work first, then call H5T_is_numeric_with_unusual_unused_bits

H5T_is_numeric_with_unusual_unused_bits(type))
HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, FAIL,
"creating dataset with unusual datatype, see documentation for "
"H5Pset_relax_file_integrity_checks for details.");
Expand Down
4 changes: 2 additions & 2 deletions src/H5Oattribute.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ H5O__attr_create(const H5O_loc_t *loc, H5A_t *attr)
HGOTO_ERROR(H5E_ATTR, H5E_CANTPIN, FAIL, "unable to pin object header");

/* Check for creating attribute with unusual datatype */
if (H5T_is_numeric_with_unusual_unused_bits(attr->shared->dt) &&
!(H5O_has_chksum(oh) || (H5F_RFIC_FLAGS(loc->file) & H5F_RFIC_UNUSUAL_NUM_UNUSED_NUMERIC_BITS)))
if (!(H5O_has_chksum(oh) || (H5F_RFIC_FLAGS(loc->file) & H5F_RFIC_UNUSUAL_NUM_UNUSED_NUMERIC_BITS)) &&
H5T_is_numeric_with_unusual_unused_bits(attr->shared->dt))
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, FAIL,
"creating attribute with unusual datatype, see documentation for "
"H5Pset_relax_file_integrity_checks for details.");
Expand Down
4 changes: 2 additions & 2 deletions src/H5Tcommit.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,8 @@ H5T__commit(H5F_t *file, H5T_t *type, hid_t tcpl_id)
HGOTO_ERROR(H5E_ATTR, H5E_CANTPIN, FAIL, "unable to pin object header");

/* Check for creating committed datatype with unusual datatype */
if (H5T_is_numeric_with_unusual_unused_bits(type) &&
!(H5O_has_chksum(oh) || (H5F_RFIC_FLAGS(file) & H5F_RFIC_UNUSUAL_NUM_UNUSED_NUMERIC_BITS)))
if (!(H5O_has_chksum(oh) || (H5F_RFIC_FLAGS(file) & H5F_RFIC_UNUSUAL_NUM_UNUSED_NUMERIC_BITS)) &&
H5T_is_numeric_with_unusual_unused_bits(type))
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL,
"creating committed datatype with unusual datatype, see documentation for "
"H5Pset_relax_file_integrity_checks for details.");
Expand Down
21 changes: 15 additions & 6 deletions src/H5private.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,21 @@
* For the time being, these can be suppressed with
* H5_GCC_CLANG_DIAG_OFF("type-limits")/H5_GCC_CLANG_DIAG_ON("type-limits")
*/
/* clang-format off */
#define H5_IS_BUFFER_OVERFLOW(ptr, size, buffer_end) \
(((ptr) > (buffer_end)) || /* Bad precondition */ \
(((size_t)(size) <= PTRDIFF_MAX) && \
((ptrdiff_t)(size) < 0)) || /* Account for (likely unintentional) negative 'size' */ \
((size_t)(size) > \
(size_t)((((const uint8_t *)buffer_end) - ((const uint8_t *)ptr)) + 1))) /* Typical overflow */
( \
/* Trivial case */ \
((size) != 0) && \
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Add check to skip buffer overflow check when size == 0

( \
/* Bad precondition */ \
((ptr) > (buffer_end)) || \
/* Account for (likely unintentional) negative 'size' */ \
(((size_t)(size) <= PTRDIFF_MAX) && ((ptrdiff_t)(size) < 0)) || \
/* Typical overflow */ \
((size_t)(size) > (size_t)((((const uint8_t *)buffer_end) - ((const uint8_t *)ptr)) + 1)) \
) \
)
/* clang-format on */

/* Variant of H5_IS_BUFFER_OVERFLOW, used with functions such as H5Tdecode()
* that don't take a size parameter, where we need to skip the bounds checks.
Expand All @@ -366,7 +375,7 @@
* the entire library.
*/
#define H5_IS_KNOWN_BUFFER_OVERFLOW(skip, ptr, size, buffer_end) \
(skip ? false : ((ptr) + (size)-1) > (buffer_end))
(skip ? false : H5_IS_BUFFER_OVERFLOW(ptr, size, buffer_end))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Update this macro to use H5_IS_BUFFER_OVERFLOW when not skipping buffer overflow check


/*
* HDF Boolean type.
Expand Down
Loading