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

Verify that the requested allocation size does not overflow. #109

Merged
merged 2 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion src/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,17 @@ void rcutils_log(
) \
size_t old_output_buffer_len = strlen(output_buffer); \
size_t required_output_buffer_size = old_output_buffer_len + n + 1; \
if (required_output_buffer_size <= old_output_buffer_len) { \
Copy link
Member

Choose a reason for hiding this comment

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

Is this the overflow case? If so a comment would be good to indicate this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is the overflow case. I can add a comment.

fprintf(stderr, "required buffer for logging output too large\n"); \
goto cleanup; \
} \
if (required_output_buffer_size > output_buffer_size) { \
do { \
output_buffer_size *= 2; \
if (output_buffer_size <= (SIZE_MAX / 2)) { \
output_buffer_size *= 2; \
} else { \
output_buffer_size = SIZE_MAX; \
} \
} while (required_output_buffer_size > output_buffer_size); \
if (output_buffer == static_output_buffer) { \
void * dynamic_output_buffer = g_rcutils_logging_allocator.allocate( \
Expand Down
7 changes: 7 additions & 0 deletions src/string_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern "C"

#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include "./common.h"
Expand Down Expand Up @@ -166,6 +167,12 @@ rcutils_string_map_reserve(rcutils_string_map_t * string_map, size_t capacity)
// note that realloc when the pointer is NULL is the same as malloc
// note also that realloc will shrink the space if needed

// ensure that reallocate won't overflow capacity
if (capacity > (SIZE_MAX / sizeof(char *))) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain in what cases this might occur?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not trace down all the callers of rcutils_string_map_reserve so it may not occur in practice today. However, it may occur if the requested capacity is greater than SIZE_MAX / 4 or SIZE_MAX / 8, depending on the pointer size.

I'll also note that glibc provides an explicit reallocarray() function that is the same as realloc() but includes the overflow check as done here.

Copy link
Member

Choose a reason for hiding this comment

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

We can consider adding a wrapper function in rcutils/allocator.h to preform this check if you like, I already created a reallocf function along these lines:

  • /// Emulate the behavior of [reallocf](https://linux.die.net/man/3/reallocf).
    /**
    * This function will return `NULL` if the allocator is `NULL` or has `NULL` for
    * function pointer fields.
    */
    RCUTILS_PUBLIC
    RCUTILS_WARN_UNUSED
    void *
    rcutils_reallocf(void * pointer, size_t size, rcutils_allocator_t * allocator);
  • rcutils/src/allocator.c

    Lines 100 to 117 in 244fa98

    void *
    rcutils_reallocf(void * pointer, size_t size, rcutils_allocator_t * allocator)
    {
    if (!rcutils_allocator_is_valid(allocator)) {
    // cannot deallocate pointer, so print message to stderr and return NULL
    #if RCUTILS_REPORT_ERROR_HANDLING_ERRORS
    RCUTILS_SAFE_FWRITE_TO_STDERR(
    "[rcutils|allocator.c:" RCUTILS_STRINGIFY(__LINE__) "] rcutils_reallocf(): "
    "invalid allocator or allocator function pointers, memory leaked\n");
    #endif
    return NULL;
    }
    void * new_pointer = allocator->reallocate(pointer, size, allocator->state);
    if (NULL == new_pointer) {
    allocator->deallocate(pointer, allocator->state);
    }
    return new_pointer;
    }

Otherwise, this check here seems fine.

RCUTILS_SET_ERROR_MSG("requested capacity for string_map too large", allocator)
return RCUTILS_RET_BAD_ALLOC;
}

// resize the keys, assigning the result only if it succeeds
char ** new_keys =
allocator.reallocate(string_map->impl->keys, capacity * sizeof(char *), allocator.state);
Expand Down