diff options
author | Gaurav Shah <gauravsh@chromium.org> | 2010-07-16 14:59:57 -0700 |
---|---|---|
committer | Gaurav Shah <gauravsh@chromium.org> | 2010-07-16 14:59:57 -0700 |
commit | aa92c634028dd883110f4ef3b103af7b684c46a5 (patch) | |
tree | 7ef0d52e315f645e5e95931cefd6948c27ff4127 /firmware/stub | |
parent | b2b0fcc0f62fadce6f854bf14826a9778c0f7632 (diff) | |
download | vboot-aa92c634028dd883110f4ef3b103af7b684c46a5.tar.gz |
Fix SafeMemcmp by removing any potential data-dependent branches.
Credit: Nate Lawson of Root Labs
Review URL: http://codereview.chromium.org/2957014
Diffstat (limited to 'firmware/stub')
-rw-r--r-- | firmware/stub/utility_stub.c | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/firmware/stub/utility_stub.c b/firmware/stub/utility_stub.c index 1445008c..3e17f773 100644 --- a/firmware/stub/utility_stub.c +++ b/firmware/stub/utility_stub.c @@ -60,14 +60,18 @@ void* Memset(void* d, const uint8_t c, uint64_t n) { return dest; } + 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* us2 = s2; - while (n--) { - if (*us1++ != *us2++) - match = 1; - } + /* Code snippet without data-dependent branch due to + * Nate Lawson (nate@root.org) of Root Labs. */ + while (n--) + result |= *us1++ ^ *us2++; - return match; + return result != 0; } |