Skip to content

Native Format Parsing - Integer Overflow Resizing Arrays#33024

Closed
varadarajkumar wants to merge 7 commits intoClickHouse:masterfrom
ClibMouse:Issue20
Closed

Native Format Parsing - Integer Overflow Resizing Arrays#33024
varadarajkumar wants to merge 7 commits intoClickHouse:masterfrom
ClibMouse:Issue20

Conversation

@varadarajkumar
Copy link
Copy Markdown
Contributor

@varadarajkumar varadarajkumar commented Dec 21, 2021

Changelog category (leave one):

  • Bug Fix (user-visible misbehaviour in official stable or prestable release)

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Integer overflow to resize the arrays causes heap corrupt

Detailed description / Documentation draft:
Problem Description:
Attacker can take advantage and abuse the overflow of integer to write to the heap.
the argument - num_elements to function byte_size function which is a 64-bit unsigned integer on the machines and it can
overflow due to the multiplication by ELEMENT_SIZE and/or the addition of the padding sizes.
The result of this arithmetic is passed to the allocation (realloc) that is expected to hold
num_elements items. If an overflow occurred during these arithmetic operations, then the
PODArray instance will believe the array can hold a much larger amount than was actually
allocated. When those items are initialized they will overwrite adjacent data on the heap.

Solution proposed:
As part of fix to address Integer overflow, proper check added in PODArray.h to prevent integer overflow conditions prior to allocations or copy operations using the results of the calculations. and also Throwing exception when CH requests for larger than MAX_SIZE.

Committer: Rajkumar Varada

@robot-clickhouse robot-clickhouse added the pr-bugfix Pull request with bugfix, not backported by default label Dec 21, 2021
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Dec 21, 2021

CLA assistant check
All committers have signed the CLA.


static size_t byte_size(size_t num_elements)
{
if (num_elements > SIZE_MAX/ELEMENT_SIZE)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Better to use __builtin_mul_overflow for this purpose.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi Alexey, I have updated the code as per your suggestion. Thanks

@alexey-milovidov alexey-milovidov self-assigned this Dec 21, 2021
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Dec 22, 2021
static size_t byte_size(size_t num_elements)
{
size_t test;
if (__builtin_mul_overflow(num_elements,ELEMENT_SIZE, &test))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (__builtin_mul_overflow(num_elements,ELEMENT_SIZE, &test))
if (__builtin_mul_overflow(num_elements, ELEMENT_SIZE, &test))

if (__builtin_mul_overflow(num_elements,ELEMENT_SIZE, &test))
throw Exception("Amount of memory requested to allocate is more than allowed", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
else
return (num_elements * ELEMENT_SIZE);
Copy link
Copy Markdown
Member

@alexey-milovidov alexey-milovidov Dec 22, 2021

Choose a reason for hiding this comment

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

Suggested change
return (num_elements * ELEMENT_SIZE);
return num_elements * ELEMENT_SIZE;

{
size_t test;
if (__builtin_mul_overflow(num_elements,ELEMENT_SIZE, &test))
throw Exception("Amount of memory requested to allocate is more than allowed", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
throw Exception("Amount of memory requested to allocate is more than allowed", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
throw Exception("Amount of memory requested to allocate is more than allowed", ErrorCodes::CANNOT_ALLOCATE_MEMORY);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @alexey-milovidov, Thanks, updated the code now.

if (__builtin_mul_overflow(num_elements,ELEMENT_SIZE, &test))
throw Exception("Amount of memory requested to allocate is more than allowed", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
else
return (num_elements * ELEMENT_SIZE);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why don't return test?

@alexey-milovidov alexey-milovidov mentioned this pull request Dec 22, 2021
alexey-milovidov added a commit that referenced this pull request Dec 23, 2021
@alexey-milovidov
Copy link
Copy Markdown
Member

Merged here: #33024

robot-clickhouse pushed a commit that referenced this pull request Dec 23, 2021
robot-clickhouse pushed a commit that referenced this pull request Dec 23, 2021
robot-clickhouse pushed a commit that referenced this pull request Dec 23, 2021
robot-clickhouse pushed a commit that referenced this pull request Dec 23, 2021
robot-clickhouse pushed a commit that referenced this pull request Dec 23, 2021
alexey-milovidov added a commit that referenced this pull request Dec 23, 2021
alexey-milovidov added a commit that referenced this pull request Dec 23, 2021
alexey-milovidov added a commit that referenced this pull request Dec 23, 2021
alexey-milovidov added a commit that referenced this pull request Dec 23, 2021
alexey-milovidov added a commit that referenced this pull request Dec 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants