libnftables: drop gmp_init() and mp_set_memory_functions()
PATCH 1/2 libnftables: drop gmp_init() and mp_set_memory_functions()
Setting global handles for libgmp via mp_set_memory_functions() is very ugly. When we don't use mini-gmp, then potentially there are other users of the library in the same process, and every process fighting about the allocation functions is not gonna work.
It also means, we must not reset the allocation functions after somebody already allocated GMP data with them. Which we cannot ensure, as we don't know what other parts of the process are doing.
It's also unnecessary. The default allocation functions for gmp and mini-gmp already abort the process on allocation failure ([1], [2]), just like our xmalloc().
Just don't do this.
[1] https://gmplib.org/repo/gmp/file/8225bdfc499f/memory.c#l37
[2] https://git.netfilter.org/nftables/tree/src/mini-gmp.c?id=6d19a902c1d77cb51b940b1ce65f31b1cad38b74#n286
--- include/nftables.h | 1 - src/gmputil.c | 10 ---------- src/libnftables.c | 1 - 3 files changed, 12 deletions(-)
include/nftables.h +0 -1
@@ -224,7 +224,6 @@ struct input_descriptor {void ct_label_table_init (struct nft_ctx *ctx);void mark_table_init (struct nft_ctx *ctx); -void gmp_init (void );void realm_table_rt_init (struct nft_ctx *ctx);void devgroup_table_init (struct nft_ctx *ctx);void xt_init (void );
src/gmputil.c +0 -10
@@ -197,13 +197,3 @@ int mpz_vfprintf(FILE *fp, const char *f, va_list args) return n; }#endif - -static void *gmp_xrealloc (void *ptr,size_t old_size,size_t new_size) -{ -return xrealloc (ptr, new_size); -} - -void gmp_init (void ) -{ -mp_set_memory_functions (xmalloc, gmp_xrealloc,NULL ); -}
src/libnftables.c +0 -1
@@ -196,7 +196,6 @@ struct nft_ctx *nft_ctx_new(uint32_t flags)if (!init_once) { init_once = true; -gmp_init ();#ifdef HAVE_LIBXTABLES xt_init ();#endif
Signature · 1 line
2.41.0
PATCH 2/2 libnftables: move init-once guard inside xt_init()
A library should not restrict being used by multiple threads or make assumptions about how it's being used. Hence a "init_once" pattern without no locking is racy, a code smell and should be avoided.
Note that libxtables is full of global variables and when linking against it, libnftables cannot be used from multiple threads either. That is not easy to fix.
Move the ugliness of "init_once" away from nft_ctx_new(), so that the problem is concentrated closer to libxtables.
--- src/libnftables.c | 6 +----- src/xt.c | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 7 deletions(-)
src/libnftables.c +1 -5
@@ -191,15 +191,11 @@ void nft_ctx_clear_include_paths(struct nft_ctx *ctx)EXPORT_SYMBOL (nft_ctx_new);struct nft_ctx *nft_ctx_new (uint32_t flags) { -static bool init_once;struct nft_ctx *ctx; -if (!init_once) { - init_once = true;#ifdef HAVE_LIBXTABLES -xt_init (); +xt_init ();#endif - } ctx =xzalloc (sizeof (struct nft_ctx ));nft_init (ctx);
src/xt.c +13 -2
@@ -361,7 +361,18 @@ static struct xtables_globals xt_nft_globals = {void xt_init (void ) { -/* Default to IPv4, but this changes in runtime */ -xtables_init_all (&xt_nft_globals,NFPROTO_IPV4 ); +static bool init_once; + +if (!init_once) { +/* libxtables is full of global variables and cannot be used +* concurrently by multiple threads. Hence, it's fine that the +* "init_once" guard is not thread-safe either. +* Don't link against xtables if you want thread safety. +*/ + init_once = true; + +/* Default to IPv4, but this changes in runtime */ +xtables_init_all (&xt_nft_globals,NFPROTO_IPV4 ); + } }#endif
Signature · 1 line
2.41.0
Pablo Neira Ayuso
maintainerRe: [PATCH nft 2/2] libnftables: move init-once guard inside xt_init()
Quoted 10 lines · Thomas Haller
A library should not restrict being used by multiple threads or make assumptions about how it's being used. Hence a "
init_once" pattern without no locking is racy, a code smell and should be avoided.Note that libxtables is full of global variables and when linking against it, libnftables cannot be used from multiple threads either. That is not easy to fix.
Move the ugliness of "
init_once" away fromnft_ctx_new(), so that the problem is concentrated closer to libxtables.
Also applied, thanks
Quoted 9 lines · Thomas Haller
Setting global handles for libgmp via
mp_set_memory_functions()is very ugly. When we don't use mini-gmp, then potentially there are other users of the library in the same process, and every process fighting about the allocation functions is not gonna work.It also means, we must not reset the allocation functions after somebody already allocated GMP data with them. Which we cannot ensure, as we don't know what other parts of the process are doing.
Quoted 5 lines
It's also unnecessary. The default allocation functions for gmp and mini-gmp already abort the process on allocation failure ([1], [2]), just like our
xmalloc().Just don't do this.
Applied, thanks