Fix SafeMemcmp by removing any potential data-dependent branches.

Credit: Nate Lawson of Root Labs

Review URL: http://codereview.chromium.org/2957014
This commit is contained in:
Gaurav Shah
2010-07-16 14:59:57 -07:00
parent b2b0fcc0f6
commit aa92c63402
2 changed files with 11 additions and 7 deletions

View File

@@ -60,14 +60,18 @@ void* Memset(void* d, const uint8_t c, uint64_t n) {
return dest; return dest;
} }
int SafeMemcmp(const void* s1, const void* s2, size_t n) { int SafeMemcmp(const void* s1, const void* s2, size_t n) {
int match = 0; int result = 0;
if (0 == n)
return 1;
const unsigned char* us1 = s1; const unsigned char* us1 = s1;
const unsigned char* us2 = s2; const unsigned char* us2 = s2;
while (n--) { /* Code snippet without data-dependent branch due to
if (*us1++ != *us2++) * Nate Lawson (nate@root.org) of Root Labs. */
match = 1; while (n--)
} result |= *us1++ ^ *us2++;
return match; return result != 0;
} }

View File

@@ -1 +1 @@
char* VbootVersion = "VBOOv=43853f81"; char* VbootVersion = "VBOOv=de3135df";