Commit 00830ddc36 for strongswan.org
commit 00830ddc36b9693fc6db68f43c18dfa42f5ca1ff
Author: Tobias Brunner <tobias@strongswan.org>
Date: Mon Jul 13 18:05:42 2026 +0200
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.
diff --git a/src/libcharon/sa/ikev2/tasks/child_create.c b/src/libcharon/sa/ikev2/tasks/child_create.c
index 4649cbf8e8..e5c2c4e9b0 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 6ee98e7643..5292d75a2b 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 bdcf1f0131..64344fed50 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 c7e8ffbc8f..6877c92bda 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)
{