Skip to content

Commit

Permalink
[libc] Fix memcmp() to return proper values
Browse files Browse the repository at this point in the history
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 <bazz@bazz1.com>
Modified-by: Michael Brown <mcb30@ipxe.org>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
bazz1tv authored and mcb30 committed Jul 21, 2020
1 parent 6ec33b8 commit 0de5e60
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/tests/string_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() */
{
Expand Down

0 comments on commit 0de5e60

Please sign in to comment.