summaryrefslogtreecommitdiffstats
path: root/src/methods/common.c
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2013-11-29 06:01:32 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2013-11-29 06:03:33 +0100
commitf04696e7451f787112e35ac184ff5057f45269eb (patch)
tree5a3e5bb2550fb1dad4bba471954f80c5c5315b14 /src/methods/common.c
parent30016f8c828ae2d8191d5490cf599bbf006d024b (diff)
downloadfastd-f04696e7451f787112e35ac184ff5057f45269eb.tar
fastd-f04696e7451f787112e35ac184ff5057f45269eb.zip
Reorder check: avoid undefined behaviour due to negative or to long shifts
Diffstat (limited to 'src/methods/common.c')
-rw-r--r--src/methods/common.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/methods/common.c b/src/methods/common.c
index 7c67253..9308332 100644
--- a/src/methods/common.c
+++ b/src/methods/common.c
@@ -63,7 +63,7 @@ bool fastd_method_is_nonce_valid(fastd_context_t *ctx, const fastd_method_common
if (timespec_diff(&ctx->now, &session->receive_last) > (int)ctx->conf->reorder_time*1000)
return false;
- if (*age > ctx->conf->reorder_count)
+ if (*age > 64)
return false;
}
@@ -72,8 +72,15 @@ bool fastd_method_is_nonce_valid(fastd_context_t *ctx, const fastd_method_common
bool fastd_method_reorder_check(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_common_t *session, const uint8_t nonce[COMMON_NONCEBYTES], int64_t age) {
if (age < 0) {
- session->receive_reorder_seen >>= age;
- session->receive_reorder_seen |= (1 >> (age+1));
+ size_t shift = age < (-64) ? 64 : ((size_t)-age);
+
+ if (shift > 63)
+ session->receive_reorder_seen = 0;
+ else
+ session->receive_reorder_seen <<= shift;
+
+ session->receive_reorder_seen |= (1 << (shift-1));
+
memcpy(session->receive_nonce, nonce, COMMON_NONCEBYTES);
session->receive_last = ctx->now;
return true;