From b4508656151009a8e3bb83ae60b25d14556aa543 Mon Sep 17 00:00:00 2001 From: Dmitriy Alexandrov Date: Mon, 14 Nov 2022 14:09:52 +0400 Subject: [PATCH] child-cfg: Fix apply_jitter() in case jitter is bigger than rekey value Also avoid returning 0 and disabling rekeying in the rare case of `jitter = rekey` and the `1/jitter` chance of that happening (returning 1 at least doesn't disable rekeying). Co-authored-by: Tobias Brunner Closes strongswan/strongswan#1414 --- src/libcharon/config/child_cfg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcharon/config/child_cfg.c b/src/libcharon/config/child_cfg.c index bc9cff712..837495c59 100644 --- a/src/libcharon/config/child_cfg.c +++ b/src/libcharon/config/child_cfg.c @@ -435,7 +435,7 @@ static uint64_t apply_jitter(uint64_t rekey, uint64_t jitter) return rekey; } jitter = (jitter == UINT64_MAX) ? jitter : jitter + 1; - return rekey - jitter * (random() / (RAND_MAX + 1.0)); + return rekey - (uint64_t)(min(jitter, rekey) * (random() / (RAND_MAX + 1.0))); } #define APPLY_JITTER(l) l.rekey = apply_jitter(l.rekey, l.jitter)