From 00830ddc36b9693fc6db68f43c18dfa42f5ca1ff Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Mon, 13 Jul 2026 18:05:42 +0200 Subject: [PATCH] ikev2: Make sure to correctly compare nonces of unequal length The previous code would not correctly compare nonces of unequal length where the shorter is the prefix of the longer one. --- src/libcharon/sa/ikev2/tasks/child_create.c | 3 +-- src/libcharon/sa/ikev2/tasks/child_rekey.c | 3 +-- src/libcharon/sa/ikev2/tasks/ike_init.c | 3 +-- src/libcharon/sa/ikev2/tasks/ike_rekey.c | 3 +-- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/libcharon/sa/ikev2/tasks/child_create.c b/src/libcharon/sa/ikev2/tasks/child_create.c index 4649cbf8e..e5c2c4e9b 100644 --- a/src/libcharon/sa/ikev2/tasks/child_create.c +++ b/src/libcharon/sa/ikev2/tasks/child_create.c @@ -2957,8 +2957,7 @@ METHOD(child_create_t, get_config, child_cfg_t*, METHOD(child_create_t, get_lower_nonce, chunk_t, private_child_create_t *this) { - if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr, - min(this->my_nonce.len, this->other_nonce.len)) < 0) + if (chunk_compare_prefix(this->my_nonce, this->other_nonce) < 0) { return this->my_nonce; } diff --git a/src/libcharon/sa/ikev2/tasks/child_rekey.c b/src/libcharon/sa/ikev2/tasks/child_rekey.c index 6ee98e764..5292d75a2 100644 --- a/src/libcharon/sa/ikev2/tasks/child_rekey.c +++ b/src/libcharon/sa/ikev2/tasks/child_rekey.c @@ -564,8 +564,7 @@ static bool lost_collision(private_child_rekey_t *this) this_nonce = this->child_create->get_lower_nonce(this->child_create); other_nonce = other->child_create->get_lower_nonce(other->child_create); - return memcmp(this_nonce.ptr, other_nonce.ptr, - min(this_nonce.len, other_nonce.len)) < 0; + return chunk_compare_prefix(this_nonce, other_nonce) < 0; } /** diff --git a/src/libcharon/sa/ikev2/tasks/ike_init.c b/src/libcharon/sa/ikev2/tasks/ike_init.c index bdcf1f013..64344fed5 100644 --- a/src/libcharon/sa/ikev2/tasks/ike_init.c +++ b/src/libcharon/sa/ikev2/tasks/ike_init.c @@ -1493,8 +1493,7 @@ METHOD(task_t, destroy, void, METHOD(ike_init_t, get_lower_nonce, chunk_t, private_ike_init_t *this) { - if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr, - min(this->my_nonce.len, this->other_nonce.len)) < 0) + if (chunk_compare_prefix(this->my_nonce, this->other_nonce) < 0) { return this->my_nonce; } diff --git a/src/libcharon/sa/ikev2/tasks/ike_rekey.c b/src/libcharon/sa/ikev2/tasks/ike_rekey.c index c7e8ffbc8..6877c92bd 100644 --- a/src/libcharon/sa/ikev2/tasks/ike_rekey.c +++ b/src/libcharon/sa/ikev2/tasks/ike_rekey.c @@ -582,8 +582,7 @@ static bool collision_lost(private_ike_rekey_t *this, bool multi_ke) /* the SA with the lowest nonce should be deleted (if already complete), * check if we or the peer created that */ - if (memcmp(this_nonce.ptr, other_nonce.ptr, - min(this_nonce.len, other_nonce.len)) < 0) + if (chunk_compare_prefix(this_nonce, other_nonce) < 0) { if (multi_ke) {