GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix for issue 775 by dcleblanc · Pull Request #776 · msgpack/msgpack-c

Fix for issue 775 #776

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.

Sign up for GitHub

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

Merged
redboltz merged 8 commits into msgpack:master from dcleblanc:master
May 12, 2019

Uh oh!

There was an error while loading. Please reload this page.

Merged

Fix for issue 775 #776

Changes from 4 commits
Commits
Show all changes
8 commits
Select commit Hold shift + click to select a range
2deed25
Fix for issue 775
Apr 11, 2019
ed30252
Spaces not tabs
Apr 11, 2019
eff6f5a
Two more tabs
Apr 11, 2019
fadc615
Fix typo
Apr 19, 2019
2d54c0e
Change integer overflow check to conform with spec
May 7, 2019
a2f3689
Older compilers don't allow declaring variables other than at the top of
May 7, 2019
fcf89fe
Fix tabs, also attempt work-around for compile time constant conditional
May 9, 2019
7a70d74
Use a #define to only check for integer overflow when it is actually
May 9, 2019
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

24 changes: 20 additions & 4 deletions 24 src/unpack.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,27 @@ static inline int template_callback_false(unpack_user* u, msgpack_object* o)
static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_object* o)
{
unsigned int size;
unsigned long long tmp;

o->type = MSGPACK_OBJECT_ARRAY;
o->via.array.size = 0;
size = n*sizeof(msgpack_object);
if (size / sizeof(msgpack_object) != n) {
tmp = (unsigned long long)n * sizeof(msgpack_object);

if (tmp & 0xffffffff00000000) {
Comment thread Outdated

@redboltz redboltz Apr 30, 2019

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Sorry for my late reply. I considered many cases.

I wrote a small test code to check sizeof types.

#include <msgpack.hpp>
#include <iostream>

int main() {
    std::cout << "msgpack_object    : " << sizeof(msgpack_object) << std::endl;
    std::cout << "size_t            : " << sizeof(size_t) << std::endl;
    std::cout << "unsigned int      : " << sizeof(unsigned int) << std::endl;
    std::cout << "unsigned long long: " <<sizeof(unsigned long long) << std::endl;
}

On 32bit environment, the output is:

msgpack_object    : 16
size_t            : 4
unsigned int      : 4
unsigned long long: 8

On 64bit environment, the output is:

msgpack_object    : 24
size_t            : 8
unsigned int      : 4
unsigned long long: 8

The max value of n is 0xffffffff. See https://github.com/msgpack/msgpack/blob/master/spec.md#array-format-family
msgpack_zone_malloc()'s 2nd parameter type is size_t.
Your check is valid on 32bit environment.

However, on 64bit environment, tmp could be greater than 0xffffffff and msgpack_zone_malloc() should be accept it. So I think that your check is false positive in this case.

What do you think?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@dcleblanc dcleblanc May 1, 2019

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The type of sizeof(msgpack_object) might be 64-bit, but the value is going to much smaller, far less than 2^32-1. And n is an unsigned int. It is true that a compiler implementation could exist where sizeof(unsigned int) > 4, but we don't have that now. A static_assert for that might be helpful, or we can also just use the SafeInt library, and let it do the right thing.

But if sizeof(unsigned int) == 4, then our maximum possible value going into tmp is this:

(2^32 - 1) * sizeof(msgpack_object), which then expands to:

2^32 * sizeof(msgpack_object) - sizeof(msgpack_object)

The only way this can possibly overrun the capacity of tmp, which is 2^64-1, is for sizeof(msgpack_object) > 2^32 (plus a bit more). If that were true, then the code can't possibly run on 32-bits, and it does, so we can't reach this condition.

Then we get to what I think your objection is, which is that on 64-bit, we could allocate more than 4GB, and this code disallows that. I had assumed that we should disallow that because the type of n is 32-bit. If you are not going to disallow that, then we need to audit all the rest of the code to ensure that nothing else is going to get truncated if it does go over 4GB.

If we're really sure that all of the code properly handles huge packages (do we have a use case for this?), then we should change the type of n to size_t, and if we do that, then I do need a different check for overflow.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@redboltz redboltz May 2, 2019

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think that we don't need to care about tmp overflow case too.

I'm sorry I looked over unsigned int size; (original code). It is too small type to treat size type. I think that it should be size_t size;. msgpack_zone_malloc()'s parameter type is size_t. https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/zone.h#L106
That is important point.

If unsigned int size; is replaced with size_t size;, if (tmp & 0xffffffff00000000) { could be false positive. That is I want to say.

On 64bit environment I mentioned above, size_t and unsigned long long has the same size.
In the case n is 0x1000 0000 and sizeof(msgpack_object) is 24, tmp is 6,442,450,944 (0x1 8000 0000). It is about 6GB. It happens on actual usecase. But if (tmp & 0xffffffff00000000) { is evaluated to true.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@dcleblanc dcleblanc May 6, 2019

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

If we change size from unsigned int to size_t, then we also have a lot of other checking to do to ensure that we're not truncating somewhere. That can be done, but do we actually have use cases for 4GB messages? If so, then let's go do the rest of the work. Is there a spec for the overall format?

Also, if we can compile this as C++, we can just use SafeInt to check overflows, but I don't know whether that's OK.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@dcleblanc dcleblanc May 6, 2019

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

So, from looking at it, I'm not thrilled with the specification. The size limits can be some trouble - from https://github.com/msgpack/msgpack/blob/master/spec.md#limitation, seems that the size of some individual items can be up to 4GB, and you can have 2^32-1 number of items in an array, so put the two of those together and we need the entire 64-bit space to address them. Which we clearly cannot do in 32 bits, and we actually can't allocate that much on any real system. But from the spec, we just have to be limited by what we can allocate, which can be tricky. We also need to be very careful to check for overflows on 32-bit systems. If we can go to C++, then we can use the new std integer types that specify sizes, and aren't compiler dependent.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@dcleblanc dcleblanc May 6, 2019

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

BTW, if we can use these, that would really help - like so std::int64_t - otherwise, some weird compiler will end up biting you. I just got a bug for SafeInt for version 3.0.18 that was fixed in 3.0.20 because I adopted these standard types. But that means C++.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@redboltz redboltz May 6, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

So, from looking at it, I'm not thrilled with the specification. The size limits can be some trouble - from https://github.com/msgpack/msgpack/blob/master/spec.md#limitation, seems that the size of some individual items can be up to 4GB, and you can have 2^32-1 number of items in an array, so put the two of those together and we need the entire 64-bit space to address them.

MessagePack format contains a kind of composite (recursive) types. And the limit of recursion level (depth) is unspecified. That means the format allows ARRAY of ARRAY of ARRAY of ... (infinity). For C++ (not C), user can define limits https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/v1/unpack_decl.hpp#L89

Here are MessagePack types that have up to 32bit length.

type contant type
ARRAY MessagePack(all types)
MAP MessagePack(all types)
STR byte
BIN byte
EXT byte
  • EXT's maximum bytes is 0xffffffff+1. See - Added unpack limit. #175

I think that only ARRAY and MAP require size_t type. They have any type of children. But the implementation always allocates sizeof(msgpack_object). So we can calculate the total size from n and sizeof(msgpack_object).

+------------+------+---------------+---------------+---------------+---------------+
|ARRAY header|length|msgpack_object |msgpack_object |msgpack_object |msgpack_object |
+------------+------+---------------+---------------+---------------+---------------+
                           |               |               |               |
                           |               |               |               |
                           |               |               |               V
       These are allocated |               |               V           +------+
       at different timing |               V           +-------+       |      |
                           V          +---------+      |       |       +------+
                     +----------+     |         |      +-------+
                     |          |     +---------+
                     +----------+

I'm not 100% sure but replacing unsigned int with size_t at ARRAY and MAP and then add appropriate integer overflow checking is a practical approach. In the checking, we don't need to care the (possible) MessagePack format limit. We can depend on OS limit (in C, malloc returns NULL).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
// integer overflow
return MSGPACK_UNPACK_NOMEM_ERROR;
}

size = (unsigned int)tmp;

if (*u->z == NULL) {
*u->z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
if(*u->z == NULL) {
return MSGPACK_UNPACK_NOMEM_ERROR;
}
}

// Unsure whether size = 0 should be an error, and if so, what to return
o->via.array.ptr = (msgpack_object*)msgpack_zone_malloc(*u->z, size);
if(o->via.array.ptr == NULL) { return MSGPACK_UNPACK_NOMEM_ERROR; }
return 0;
Expand All @@ -223,19 +231,27 @@ static inline int template_callback_array_item(unpack_user* u, msgpack_object* c
static inline int template_callback_map(unpack_user* u, unsigned int n, msgpack_object* o)
{
unsigned int size;
unsigned long long tmp;

o->type = MSGPACK_OBJECT_MAP;
o->via.map.size = 0;
size = n*sizeof(msgpack_object_kv);
if (size / sizeof(msgpack_object_kv) != n) {
tmp = (unsigned long long)n * sizeof(msgpack_object_kv);

if (tmp & 0xffffffff00000000) {
// integer overflow
return MSGPACK_UNPACK_NOMEM_ERROR;
}

size = (unsigned int)tmp;

if (*u->z == NULL) {
*u->z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
if(*u->z == NULL) {
return MSGPACK_UNPACK_NOMEM_ERROR;
}
}

// Should size = 0 be an error? If so, what error to return?
o->via.map.ptr = (msgpack_object_kv*)msgpack_zone_malloc(*u->z, size);
if(o->via.map.ptr == NULL) { return MSGPACK_UNPACK_NOMEM_ERROR; }
return 0;
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.