diff options
author | jsing <> | 2024-01-24 14:02:52 +0000 |
---|---|---|
committer | jsing <> | 2024-01-24 14:02:52 +0000 |
commit | 8cf7a8d06866dd151552f63a2f8bf289d3e48683 (patch) | |
tree | 23db63fc3b4137172ed05920d323daacd2d92049 /src/lib/libcrypto/lhash | |
parent | 304c4bea814902359fc956dde611b1bf4fe6c276 (diff) | |
download | openbsd-8cf7a8d06866dd151552f63a2f8bf289d3e48683.tar.gz openbsd-8cf7a8d06866dd151552f63a2f8bf289d3e48683.tar.bz2 openbsd-8cf7a8d06866dd151552f63a2f8bf289d3e48683.zip |
Make it safe to delete entries from an lhash doall callback.
Currently, the callback cannot safely delete entries as it could lead to
contraction of the hash table, which in turn could lead to doall skipping
entries (and that typically leads to memory leaks). The recommended
workaround is to reach in and fiddle with the hash table internals in
order to prevent contraction, call the doall function and then restore
the internals that were changed.
Rather than just improving our documentation, actually make it safe to
delete entries from an lhash doall callback by pausing contractions prior
to starting the callback loop, then restoring the down load factor and
triggering contraction once completed. This means that callers no longer
need access to change hash table internals in order to achieve this same
behaviour.
ok tb@
Diffstat (limited to 'src/lib/libcrypto/lhash')
-rw-r--r-- | src/lib/libcrypto/lhash/lhash.c | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/lib/libcrypto/lhash/lhash.c b/src/lib/libcrypto/lhash/lhash.c index 3adec71ed6..81660419c7 100644 --- a/src/lib/libcrypto/lhash/lhash.c +++ b/src/lib/libcrypto/lhash/lhash.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: lhash.c,v 1.20 2023/07/07 13:40:44 beck Exp $ */ | 1 | /* $OpenBSD: lhash.c,v 1.21 2024/01/24 14:02:52 jsing Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -250,12 +250,21 @@ static void | |||
250 | doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func, | 250 | doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func, |
251 | LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg) | 251 | LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg) |
252 | { | 252 | { |
253 | int i; | ||
254 | LHASH_NODE *a, *n; | 253 | LHASH_NODE *a, *n; |
254 | int down_load; | ||
255 | int i; | ||
255 | 256 | ||
256 | if (lh == NULL) | 257 | if (lh == NULL) |
257 | return; | 258 | return; |
258 | 259 | ||
260 | /* | ||
261 | * Disable contraction of the hash while walking, as some consumers use | ||
262 | * it to delete hash entries. A better option would be to snapshot the | ||
263 | * hash, making it insert safe as well. | ||
264 | */ | ||
265 | down_load = lh->down_load; | ||
266 | lh->down_load = 0; | ||
267 | |||
259 | /* reverse the order so we search from 'top to bottom' | 268 | /* reverse the order so we search from 'top to bottom' |
260 | * We were having memory leaks otherwise */ | 269 | * We were having memory leaks otherwise */ |
261 | for (i = lh->num_nodes - 1; i >= 0; i--) { | 270 | for (i = lh->num_nodes - 1; i >= 0; i--) { |
@@ -273,6 +282,10 @@ doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func, | |||
273 | a = n; | 282 | a = n; |
274 | } | 283 | } |
275 | } | 284 | } |
285 | |||
286 | /* Restore down load factor and trigger contraction. */ | ||
287 | lh->down_load = down_load; | ||
288 | contract(lh); | ||
276 | } | 289 | } |
277 | 290 | ||
278 | void | 291 | void |