Skip to content

tinycbor buffer overflow causing mcumgr image upload failure #19629

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

Closed
jimparis opened this issue Oct 4, 2019 · 4 comments · Fixed by #19838
Closed

tinycbor buffer overflow causing mcumgr image upload failure #19629

jimparis opened this issue Oct 4, 2019 · 4 comments · Fixed by #19838
Assignees
Labels
bug The issue is a bug, or the PR is fixing a bug priority: high High impact/importance bug

Comments

@jimparis
Copy link
Collaborator

jimparis commented Oct 4, 2019

Hi,
Reporting this here because zephyrproject-rtos/tinycbor doesn't have an issue tracker.

There is a buffer overflow in zephyr's tinycbor, in cborparser.c:

CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
                                 size_t *buflen, CborValue *next)
{
    //...
    CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
                                          buffer ? (IterateFunction) value->parser->d->cpy : iterate_noop);
    //...
    if (buffer) {
        *((uint8_t *)buffer + *buflen) = '\0';
    }

If iterate_string_chunks fills the buffer completely, the byte past the buffer is zeroed. This is in contrast to the function comment, which says "If the buffer is large enough, this function will insert a null byte after the last copied byte".

The bug is not present in the upstream intel/tinycbor, where they add termination inside iterate_string_chunks instead.

The bug was introduced by zephyrproject-rtos/tinycbor#1, which appears intended to bring the code more in-line with the upstream mynewt tinycbor, but their code doesn't have the bug either.


The specific symptom I'm seeing is:

$ mcumgr --conntype serial --connstring dev=/dev/foo image upload foo.bin
Error: 3

and it's happening because data_sha overflows into img_mgmt_data in img_mgmt_upload, during a cbor_read_object call:

static int
img_mgmt_upload(struct mgmt_ctxt *ctxt)
{
    uint8_t img_mgmt_data[IMG_MGMT_UL_CHUNK_SIZE];
    uint8_t data_sha[IMG_MGMT_DATA_SHA_LEN];

which clobbers the IMAGE_MAGIC checked by img_mgmt_check_header.


This same tinycbor bug was the root cause of an earlier mcumgr fix in #7924, which ended up in a patch to upstream's mcumgr apache/mynewt-mcumgr#5. There, it was noted by @ccollins476ad that it was actually a zephyr tinycbor bug, but the workaround was merged to mynewt's mcumgr anyway (apache/mynewt-mcumgr@c2da8ca), and then also into zephyr's fork (zephyrproject-rtos/mcumgr@d9b889e).

I think both of those commits should be reverted in their repos once this is fixed.

@jimparis jimparis added the bug The issue is a bug, or the PR is fixing a bug label Oct 4, 2019
@dleach02
Copy link
Member

dleach02 commented Oct 7, 2019

@nvlsianpu, can you provide some guidance on priority level for this problem?

@nvlsianpu nvlsianpu added the priority: high High impact/importance bug label Oct 8, 2019
@nvlsianpu
Copy link
Collaborator

@jimparis zephyrproject-rtos/tinycbor#7 Is this full fix for the issue?

@jimparis
Copy link
Collaborator Author

jimparis commented Oct 8, 2019

As far as I'm aware yes, for tinycbor. Then an update to zephyr's west.yml here to point to the new version. Then the workarounds in mcumgr could be removed, but they are harmless.

@de-nordic
Copy link
Collaborator

@carlescufi is already updating west.yml manifest here: #19838

carlescufi added a commit to carlescufi/zephyr that referenced this issue Oct 16, 2019
Point to the current revision at the tip of the tinycbor repo after
merging zephyrproject-rtos/tinycbor#7.

Fixes zephyrproject-rtos#19629.

Signed-off-by: Carles Cufi <[email protected]>
carlescufi added a commit that referenced this issue Oct 16, 2019
Point to the current revision at the tip of the tinycbor repo after
merging zephyrproject-rtos/tinycbor#7.

Fixes #19629.

Signed-off-by: Carles Cufi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue is a bug, or the PR is fixing a bug priority: high High impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants