From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 18 Nov 2026 17:41:26 +0100 Subject: [PATCH] sync: bound the reconcile reply's filter shape The client allocated whatever bucket_count the server sent and accepted any k, while the filter code needs k distinct buckets per element and never terminates with fewer. Issue: https://bugs.taler.net/n/12210 Assisted by LLM --- src/include/sync/sync_ibf.h | 10 ++++++---- src/lib/sync_api_block_reconcile.c | 9 +++++++-- src/util/sync_ibf.c | 6 +++++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/include/sync/sync_ibf.h b/src/include/sync/sync_ibf.h index c3c3cf1..57e383d 100644 --- a/src/include/sync/sync_ibf.h +++ b/src/include/sync/sync_ibf.h @@ -92,11 +92,12 @@ SYNC_ibf_compute_bucket_count (uint32_t block_count); /** * Create a fresh invertible bloom filter. * - * @param bucket_count number of buckets + * @param bucket_count number of buckets, at least @a hash_num * @param hash_num number of hash functions per element * @param prefix mixing salt hashed with every element (4 bytes, * network byte order, zero for the default) - * @return the filter, NULL on failure + * @return the filter, NULL if @a bucket_count or @a hash_num is zero + * or there are fewer buckets than hash functions */ struct SYNC_IBF * SYNC_ibf_create (uint32_t bucket_count, @@ -109,10 +110,11 @@ SYNC_ibf_create (uint32_t bucket_count, * * @param data serialized filter of @a bucket_count buckets * (see #SYNC_ibf_serialize()) - * @param bucket_count number of buckets the serialized filter holds + * @param bucket_count number of buckets the serialized filter holds, + * at least @a hash_num * @param hash_num number of hash functions per element * @param prefix mixing salt the serialized filter was built with - * @return the filter, NULL on failure + * @return the filter, NULL on failure (see #SYNC_ibf_create()) */ struct SYNC_IBF * SYNC_ibf_deserialize (const void *data, diff --git a/src/lib/sync_api_block_reconcile.c b/src/lib/sync_api_block_reconcile.c index ff67462..301171d 100644 --- a/src/lib/sync_api_block_reconcile.c +++ b/src/lib/sync_api_block_reconcile.c @@ -121,11 +121,14 @@ handle_reconcile_finished (void *cls, } rd.version = (uint32_t) json_integer_value (j); /* bucket_count */ + /* bucket_count sizes the allocation below; the server never + builds more than SYNC_IBF_MAX_BUCKETS */ j = json_object_get (json, "bucket_count"); if ( (NULL == j) || (! json_is_integer (j)) || - (json_integer_value (j) <= 0) ) + (json_integer_value (j) <= 0) || + (json_integer_value (j) > SYNC_IBF_MAX_BUCKETS) ) { GNUNET_break_op (0); rd.ec = TALER_EC_GENERIC_PARAMETER_MALFORMED; @@ -133,11 +136,13 @@ handle_reconcile_finished (void *cls, } rd.bucket_count = (uint32_t) json_integer_value (j); /* k */ + /* the protocol fixes the number of hash functions; anything + else cannot be reconciled against a local filter */ j = json_object_get (json, "k"); if ( (NULL == j) || (! json_is_integer (j)) || - (json_integer_value (j) <= 0) ) + (SYNC_IBF_HASH_NUM != json_integer_value (j)) ) { GNUNET_break_op (0); rd.ec = TALER_EC_GENERIC_PARAMETER_MALFORMED; diff --git a/src/util/sync_ibf.c b/src/util/sync_ibf.c index da5e01b..f43c63c 100644 --- a/src/util/sync_ibf.c +++ b/src/util/sync_ibf.c @@ -287,8 +287,12 @@ SYNC_ibf_create (uint32_t bucket_count, { struct SYNC_IBF *ibf; + /* an element needs hash_num distinct bucket indices, so there + must be at least that many buckets or ibf_get_indices() would + never terminate */ if ( (0 == bucket_count) || - (0 == hash_num) ) + (0 == hash_num) || + (hash_num > bucket_count) ) return NULL; ibf = GNUNET_new (struct SYNC_IBF); ibf->size = bucket_count; -- 2.47.3