Skip to content

Commit acf202a

Browse files
alradmsftthiagomacieira
authored andcommitted
Fix stack corruption due to calling convention mismatch
Standard library implementations may specify calling convention for memcpy explicitly. However, the cbor* APIs don't do that. If you compile the lib with default calling convention that doesn't match the calling convention o memcpy(), the iterate_string_chunks() will not setup the stack for memcpy() call correctly resulting in a stack corruption. The compiler doesn't catch this issue because of the cast that is being applied when passing memcpy() to the iterate_string_chunks(). The fix is to wrap memcpy() in a function that conforms to the declaration o the IterateFunction which does two things, removes the need for a cast and uncouples memcpy() calling convention from the rest of the cbor* APIs. Signed-off-by: Alex Radutskiy ([email protected]) Signed-off-by: Thiago Macieira <[email protected]>
1 parent 726b786 commit acf202a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/cborparser.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,11 @@ static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len)
915915
return memcmp(s1, (const char *)s2, len) == 0;
916916
}
917917

918+
static uintptr_t iterate_memcpy(char *dest, const uint8_t *src, size_t len)
919+
{
920+
return (uintptr_t)memcpy(dest, src, len);
921+
}
922+
918923
static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
919924
bool *result, CborValue *next, IterateFunction func)
920925
{
@@ -1061,7 +1066,7 @@ CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
10611066
{
10621067
bool copied_all;
10631068
CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
1064-
buffer ? (IterateFunction)memcpy : iterate_noop);
1069+
buffer ? iterate_memcpy : iterate_noop);
10651070
return err ? err :
10661071
copied_all ? CborNoError : CborErrorOutOfMemory;
10671072
}

0 commit comments

Comments
 (0)