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

Fix memory leaks and aborts in H5O EFL decode #2656

Merged
merged 4 commits into from
Apr 3, 2023

Conversation

derobins
Copy link
Member

@derobins derobins commented Apr 2, 2023

H5O__efl_decode() would call assert() and not properly clean up memory when parsing malformed external data files messages. This change corrects that behavior and fixes #2605.

derobins added 2 commits April 2, 2023 06:46
The function that decodes external data files object header messages
would call assert() when parsing malformed files, causing applications
to crash when linked against the debug library.

This change converts these assert() calls to HDF5 error checks, so
the messages are sanity checked in both release and debug mode and
debug mode no longer crashes applications.

Also cleaned up some error handling usage and debug checks.

Related to GitHub HDFGroup#2605
@@ -111,64 +103,69 @@ H5O__efl_decode(H5F_t *f, H5O_t H5_ATTR_UNUSED *open_oh, unsigned H5_ATTR_UNUSED

/* Number of slots */
UINT16DECODE(p, mesg->nalloc);
HDassert(mesg->nalloc > 0);
if (mesg->nalloc <= 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, NULL, "bad number of allocated slots when parsing efl msg")
Copy link
Member Author

Choose a reason for hiding this comment

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

Parsing a malformed file should never result in an assert() call.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree

However, it's an unsigned value, so can't be <0

Copy link
Member Author

Choose a reason for hiding this comment

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

I could change that to be != 0, but then that would be broken if anyone changes the type to be signed so they can have a "bad" value.

for (size_t u = 0; u < mesg->nused; u++)
H5MM_xfree(mesg->slot[u].name);
H5MM_xfree(mesg->slot);
}
Copy link
Member Author

@derobins derobins Apr 2, 2023

Choose a reason for hiding this comment

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

mesg->slot and the names could be allocated but were not cleaned on errors.

Copy link
Contributor

@qkoziol qkoziol left a comment

Choose a reason for hiding this comment

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

Suggest parentheses around math expressions involved in comparisons. Memorizing the precedence of operations shouldn't be the most important thing on your mind when looking at this code.

@derobins
Copy link
Member Author

derobins commented Apr 2, 2023

Suggest parentheses around math expressions involved in comparisons. Memorizing the precedence of operations shouldn't be the most important thing on your mind when looking at this code.

I went ahead and added parens, but I honestly don't think that remembering math > logic is too much to remember. If I get some free time, I'll add a function or macro to hide all the awkward -1 stuff.

@derobins derobins added Merge - To 1.12 Priority - 1. High 🔼 These are important issues that should be resolved in the next release Component - C Library Core C library issues (usually in the src directory) Type - Bug / Bugfix Please report security issues to help@hdfgroup.org instead of creating an issue on GitHub labels Apr 2, 2023
Copy link
Contributor

@qkoziol qkoziol left a comment

Choose a reason for hiding this comment

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

Cool, thanks :-)

void *ret_value = NULL; /* Return value */
H5O_efl_t *mesg = NULL;
int version;
const uint8_t *p_end = p + p_size - 1; /* pointer to last byte in p */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggest possibly moving this assignment past the assert for p since addition on a NULL pointer is undefined. That said, we don't have a normal check for p != NULL so it won't be caught in release builds as is anyway if this ever happens. Hopefully the assert would catch it during development though.

@derobins derobins merged commit 1c1af33 into HDFGroup:develop Apr 3, 2023
derobins added a commit to derobins/hdf5 that referenced this pull request Apr 12, 2023
* Convert asserts to error handling in efl decode

The function that decodes external data files object header messages
would call assert() when parsing malformed files, causing applications
to crash when linked against the debug library.

This change converts these assert() calls to HDF5 error checks, so
the messages are sanity checked in both release and debug mode and
debug mode no longer crashes applications.

Also cleaned up some error handling usage and debug checks.

* Free memory on H5O efl decode errors

* Add buffer size checks to efl msg decode

* Add parentheses to math expressions

Fixes GitHub HDFGroup#2605
derobins added a commit to derobins/hdf5 that referenced this pull request Apr 12, 2023
* Convert asserts to error handling in efl decode

The function that decodes external data files object header messages
would call assert() when parsing malformed files, causing applications
to crash when linked against the debug library.

This change converts these assert() calls to HDF5 error checks, so
the messages are sanity checked in both release and debug mode and
debug mode no longer crashes applications.

Also cleaned up some error handling usage and debug checks.

* Free memory on H5O efl decode errors

* Add buffer size checks to efl msg decode

* Add parentheses to math expressions

Fixes GitHub HDFGroup#2605
derobins added a commit that referenced this pull request Apr 13, 2023
* Convert asserts to error handling in efl decode

The function that decodes external data files object header messages
would call assert() when parsing malformed files, causing applications
to crash when linked against the debug library.

This change converts these assert() calls to HDF5 error checks, so
the messages are sanity checked in both release and debug mode and
debug mode no longer crashes applications.

Also cleaned up some error handling usage and debug checks.

* Free memory on H5O efl decode errors

* Add buffer size checks to efl msg decode

* Add parentheses to math expressions

Fixes GitHub #2605
derobins added a commit that referenced this pull request Apr 13, 2023
* Convert asserts to error handling in efl decode

The function that decodes external data files object header messages
would call assert() when parsing malformed files, causing applications
to crash when linked against the debug library.

This change converts these assert() calls to HDF5 error checks, so
the messages are sanity checked in both release and debug mode and
debug mode no longer crashes applications.

Also cleaned up some error handling usage and debug checks.

* Free memory on H5O efl decode errors

* Add buffer size checks to efl msg decode

* Add parentheses to math expressions

Fixes GitHub #2605
brtnfld pushed a commit to brtnfld/hdf5 that referenced this pull request May 17, 2023
* Convert asserts to error handling in efl decode

The function that decodes external data files object header messages
would call assert() when parsing malformed files, causing applications
to crash when linked against the debug library.

This change converts these assert() calls to HDF5 error checks, so
the messages are sanity checked in both release and debug mode and
debug mode no longer crashes applications.

Also cleaned up some error handling usage and debug checks.

* Free memory on H5O efl decode errors

* Add buffer size checks to efl msg decode

* Add parentheses to math expressions

Fixes GitHub HDFGroup#2605
@derobins derobins deleted the fix_2605 branch March 27, 2024 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component - C Library Core C library issues (usually in the src directory) Priority - 1. High 🔼 These are important issues that should be resolved in the next release Type - Bug / Bugfix Please report security issues to help@hdfgroup.org instead of creating an issue on GitHub
Projects
Status: Merges Complete
Development

Successfully merging this pull request may close these issues.

[BUG] detected memory leaks in H5O__efl_decode hdf5/src/H5Oefl.c
4 participants