mirror of
https://gitlab.com/chrony/chrony.git
synced 2025-12-03 16:45:07 -05:00
keys: compare MACs in constant time
Switch from memcmp() to the new constant-time function to compare the received and expected authentication data generated with a symmetric key (NTP MAC or AES CMAC). While this doesn't seem to be strictly necessary with the current code, it is a recommended practice to prevent timing attacks. If memcmp() compared the MACs one byte at a time (a typical memcmp() implementation works with wider integers for better performance) and chronyd as an NTP client/server/peer was leaking the timing of the comparison (e.g. in the monitoring protocol), an attacker might be able for a given NTP request or response find in a sequence the individual bytes of the MAC by observing differences in the timing over a large number of attempts. However, this process would likely be so slow the authenticated request or response would not be useful in a MITM attack as the expected origin timestamp is changing with each poll. Extend the keys unit test to compare the time the function takes to compare two identical MACs and MACs differing in the first byte (maximizing the timing difference). It should fail if the compiler's optimizations figure out the function can return early. The test is not included in the util unit test to avoid compile-time optimizations with the function and its caller together. The test can be disabled by setting NO_TIMING_TESTS environment variable if it turns out to be unreliable.
This commit is contained in:
2
keys.c
2
keys.c
@@ -405,7 +405,7 @@ check_auth(Key *key, const void *data, int data_len,
|
||||
|
||||
hash_len = generate_auth(key, data, data_len, buf, sizeof (buf));
|
||||
|
||||
return MIN(hash_len, trunc_len) == auth_len && !memcmp(buf, auth, auth_len);
|
||||
return MIN(hash_len, trunc_len) == auth_len && UTI_IsMemoryEqual(buf, auth, auth_len);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
Reference in New Issue
Block a user