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 <[email protected]>

Closes strongswan/strongswan#1414
This commit is contained in:
Dmitriy Alexandrov
2022-12-12 14:24:32 +01:00
committed by Tobias Brunner
co-authored by Tobias Brunner
parent 44378d2521
commit b450865615
+1 -1
View File
@@ -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)