From 2f032c84a2cbed37308da8b29e84efb6eb2fe9e5 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 7 Jul 2020 13:13:28 +0100 Subject: [libc] Provide an unoptimised generic_memcpy_reverse() Signed-off-by: Michael Brown --- src/core/string.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'src/core/string.c') diff --git a/src/core/string.c b/src/core/string.c index 5bd9dae8b..c35015b59 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -52,7 +52,7 @@ void * generic_memset ( void *dest, int character, size_t len ) { } /** - * Copy memory region + * Copy memory region (forwards) * * @v dest Destination region * @v src Source region @@ -69,24 +69,39 @@ void * generic_memcpy ( void *dest, const void *src, size_t len ) { } /** - * Copy (possibly overlapping) memory region + * Copy memory region (backwards) * * @v dest Destination region * @v src Source region * @v len Length * @ret dest Destination region */ -void * generic_memmove ( void *dest, const void *src, size_t len ) { +void * generic_memcpy_reverse ( void *dest, const void *src, size_t len ) { const uint8_t *src_bytes = ( src + len ); uint8_t *dest_bytes = ( dest + len ); - if ( dest < src ) - return generic_memcpy ( dest, src, len ); while ( len-- ) *(--dest_bytes) = *(--src_bytes); return dest; } +/** + * Copy (possibly overlapping) memory region + * + * @v dest Destination region + * @v src Source region + * @v len Length + * @ret dest Destination region + */ +void * generic_memmove ( void *dest, const void *src, size_t len ) { + + if ( dest < src ) { + return generic_memcpy ( dest, src, len ); + } else { + return generic_memcpy_reverse ( dest, src, len ); + } +} + /** * Compare memory regions * -- cgit v1.2.3-55-g7522 From 0de5e601440bdb1f244b054384d6f77cefc86ba7 Mon Sep 17 00:00:00 2001 From: Michael J. Bazzinotti Date: Tue, 14 Jan 2020 13:15:02 -0500 Subject: [libc] Fix memcmp() to return proper values Fix memcmp() to return proper standard positive/negative values for unequal comparisons. Current implementation is backwards (i.e. the functions are returning negative when should be positive and vice-versa). Currently most consumers of these functions only check the return value for ==0 or !=0 and so we can safely change the implementation without breaking things. However, there is one call that checks the polarity of this function, and that is prf_sha1() for wireless WPA 4-way handshake. Due to the incorrect memcmp() polarity, the WPA handshake creates an incorrect PTK, and the handshake would fail after step 2. Undoubtedly, the AP noticed the supplicant failed the mic check. This commit fixes that issue. Similar to commit 3946aa9 ("[libc] Fix strcmp()/strncmp() to return proper values"). Signed-off-by: Michael Bazzinotti Modified-by: Michael Brown Signed-off-by: Michael Brown --- src/core/string.c | 2 +- src/tests/string_test.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'src/core/string.c') diff --git a/src/core/string.c b/src/core/string.c index c35015b59..188fe0864 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -116,7 +116,7 @@ int memcmp ( const void *first, const void *second, size_t len ) { int diff; while ( len-- ) { - diff = ( *(second_bytes++) - *(first_bytes++) ); + diff = ( *(first_bytes++) - *(second_bytes++) ); if ( diff ) return diff; } diff --git a/src/tests/string_test.c b/src/tests/string_test.c index a66501da3..88a730aec 100644 --- a/src/tests/string_test.c +++ b/src/tests/string_test.c @@ -109,6 +109,7 @@ static void string_test_exec ( void ) { ok ( memcmp ( "", "", 0 ) == 0 ); ok ( memcmp ( "Foo", "Foo", 3 ) == 0 ); ok ( memcmp ( "Foo", "Bar", 3 ) != 0 ); + ok ( memcmp ( "abc", "def", 3 ) < 0 ); /* Test strstr() */ { -- cgit v1.2.3-55-g7522