Skip to content
This repository was archived by the owner on Apr 9, 2020. It is now read-only.

Fix problem exactly filling a buffer when encoding. #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sbertin-telular
Copy link

The size check suffered from an off by one error.
This also avoids problems with overflow.

The size check suffered from an off by one error.
This also avoids problems with overflow.
Copy link
Owner

@cabo cabo left a comment

Choose a reason for hiding this comment

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

Thank you! I do have one question, though...

@@ -35,7 +35,7 @@ typedef struct _write_state
ssize_t size;
} cn_write_state;

#define ensure_writable(sz) if ((ws->offset<0) || (ws->offset + (sz) >= ws->size)) { \
#define ensure_writable(sz) if ((ws->offset<0) || (ws->size - ws->offset < (sz))) { \
Copy link
Owner

Choose a reason for hiding this comment

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

This is definitely a good change...

src/cn-encoder.c Outdated
@@ -302,6 +302,7 @@ ssize_t cn_cbor_encoder_write(uint8_t *buf,
const cn_cbor *cb)
{
cn_write_state ws = { buf, buf_offset, buf_size };
if (ws.size < 0) { return -1; }
Copy link
Owner

Choose a reason for hiding this comment

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

When would this be the case?

Copy link
Author

Choose a reason for hiding this comment

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

In normal use I would expect it would not happen, but if buf_size has the high bit set and buf_offset is non-zero the check in ensure_writable could underflow. Probably an overabundance of caution, but issue #12 made me think about what could happen in an attack situation.

Copy link
Author

Choose a reason for hiding this comment

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

Would you like to see this removed? Or maybe change ws.size from ssize_t to size_t? I'd like to see the off by one error fixed. It seems there were attempts to do so since 2015 that never quite made it.

@cabo
Copy link
Owner

cabo commented Apr 13, 2018

I generally prefer to make all these calculations unsigned so I don't run into implementation-defined issues. Unfortunately, -1 means something special for ws.offset, which probably is the reason it was defined as ssize_t. Once you mix signed and unsigned, interesting things happen in C, so if we want to change ws.size to size_t, we might have to change ws.offset, too. In any case, a ws.size of more than half of the address space doesn't sound right, so this seems to be more of an "assert" kind of check.

@jimsch
Copy link
Contributor

jimsch commented Apr 16, 2018

Also look at #25 which does the same thing

@sbertin-telular
Copy link
Author

Is anything else needed to get this merged?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants