Skip to contentSkip to conversation

utils: add memory_allocation_check() helper

PATCH In review Thomas Haller 3 participants 11 messages Nov 8, 2023 – Nov 15, 2023 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 utils: add memory_allocation_check() helper

libnftables kills the process on out of memory (xmalloc()), so when we use libraries that propagate ENOMEM to libnftables, we also abort the process.

For example:

     nlr = nftnl_rule_alloc();
     if (!nlr)
          memory_allocation_error();

Add memory_allocation_check() macro which can simplify this common check to:

     nlr = memory_allocation_check(nftnl_rule_alloc());
---
 include/utils.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/utils.h +10 -0
index 36a28f893667..fcd7c598fe9f 100644
@@ -142,6 +142,16 @@ extern void __memory_allocation_error(const char *filename, uint32_t line) __nor
 #define memory_allocation_error()		\
 	__memory_allocation_error(__FILE__, __LINE__);
 
+#define memory_allocation_check(cmd)               \
+	({                                         \
+		typeof((cmd)) _v = (cmd);          \
+		const void *const _v2 = _v;        \
+                                                   \
+		if (!_v2)                          \
+			memory_allocation_error(); \
+		_v;                                \
+	})
+
 extern void xfree(const void *ptr);
 extern void *xmalloc(size_t size);
 extern void *xmalloc_array(size_t nmemb, size_t size);
Signature · 1 line
2.41.0
#1 lore raw

PATCH 2/2 netlink: add and use _nftnl_udata_buf_alloc() helper

We don't want to handle allocation errors, but crash via memory_allocation_error(). Also, we usually just allocate NFT_USERDATA_MAXLEN buffers.

Add a helper for that and use it.

---
 include/netlink.h       |  3 +++
 src/mnl.c               | 16 ++++------------
 src/netlink.c           |  7 ++-----
 src/netlink_linearize.c |  4 +---
 4 files changed, 10 insertions(+), 20 deletions(-)
include/netlink.h +3 -0
index 6766d7e8563f..15cbb332c8dd 100644
@@ -260,4 +260,7 @@ struct nft_expr_loc *nft_expr_loc_find(const struct nftnl_expr *nle,
 
 struct dl_proto_ctx *dl_proto_ctx(struct rule_pp_ctx *ctx);
 
+#define _nftnl_udata_buf_alloc() \
+	memory_allocation_check(nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN))
+
 #endif /* NFTABLES_NETLINK_H */
src/mnl.c +4 -12
index 0fb36bd588ee..1263c611cd20 100644
@@ -823,9 +823,7 @@ int mnl_nft_chain_add(struct netlink_ctx *ctx, struct cmd *cmd,
 					    CHAIN_F_HW_OFFLOAD);
 		}
 		if (cmd->chain->comment) {
-			udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
-			if (!udbuf)
-				memory_allocation_error();
+			udbuf = _nftnl_udata_buf_alloc();
 			if (!nftnl_udata_put_strz(udbuf, NFTNL_UDATA_CHAIN_COMMENT, cmd->chain->comment))
 				memory_allocation_error();
 			nftnl_chain_set_data(nlc, NFTNL_CHAIN_USERDATA, nftnl_udata_buf_data(udbuf),
@@ -1057,9 +1055,7 @@ int mnl_nft_table_add(struct netlink_ctx *ctx, struct cmd *cmd,
 		nftnl_table_set_u32(nlt, NFTNL_TABLE_FLAGS, cmd->table->flags);
 
 		if (cmd->table->comment) {
-			udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
-			if (!udbuf)
-				memory_allocation_error();
+			udbuf = _nftnl_udata_buf_alloc();
 			if (!nftnl_udata_put_strz(udbuf, NFTNL_UDATA_TABLE_COMMENT, cmd->table->comment))
 				memory_allocation_error();
 			nftnl_table_set_data(nlt, NFTNL_TABLE_USERDATA, nftnl_udata_buf_data(udbuf),
@@ -1256,9 +1252,7 @@ int mnl_nft_set_add(struct netlink_ctx *ctx, struct cmd *cmd,
 		nftnl_set_set_u32(nls, NFTNL_SET_DESC_SIZE, set->init->size);
 	}
 
-	udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
-	if (!udbuf)
-		memory_allocation_error();
+	udbuf = _nftnl_udata_buf_alloc();
 	if (!nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_KEYBYTEORDER,
 				 set->key->byteorder))
 		memory_allocation_error();
@@ -1453,9 +1447,7 @@ int mnl_nft_obj_add(struct netlink_ctx *ctx, struct cmd *cmd,
 	nftnl_obj_set_u32(nlo, NFTNL_OBJ_TYPE, obj->type);
 
 	if (obj->comment) {
-		udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
-		if (!udbuf)
-			memory_allocation_error();
+		udbuf = _nftnl_udata_buf_alloc();
 		if (!nftnl_udata_put_strz(udbuf, NFTNL_UDATA_OBJ_COMMENT, obj->comment))
 			memory_allocation_error();
 		nftnl_obj_set_data(nlo, NFTNL_OBJ_USERDATA, nftnl_udata_buf_data(udbuf),
src/netlink.c +2 -5
index 120a8ba9ceb1..0c858065ca15 100644
@@ -175,11 +175,8 @@ struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,
 						netlink_gen_stmt_stateful(stmt));
 		}
 	}
-	if (elem->comment || expr->elem_flags) {
-		udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
-		if (!udbuf)
-			memory_allocation_error();
-	}
+	if (elem->comment || expr->elem_flags)
+		udbuf = _nftnl_udata_buf_alloc();
 	if (elem->comment) {
 		if (!nftnl_udata_put_strz(udbuf, NFTNL_UDATA_SET_ELEM_COMMENT,
 					  elem->comment))
src/netlink_linearize.c +1 -3
index 0c62341112d8..b5adc4d186c8 100644
@@ -1760,9 +1760,7 @@ void netlink_linearize_rule(struct netlink_ctx *ctx,
 	if (rule->comment) {
 		struct nftnl_udata_buf *udata;
 
-		udata = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
-		if (!udata)
-			memory_allocation_error();
+		udata = _nftnl_udata_buf_alloc();
 
 		if (!nftnl_udata_put_strz(udata, NFTNL_UDATA_RULE_COMMENT,
 					  rule->comment))
Signature · 1 line
2.41.0
#2 lore raw

Re: [PATCH nft 2/2] netlink: add and use _nftnl_udata_buf_alloc() helper

Quoted 18 lines · Thomas Haller

We don't want to handle allocation errors, but crash via memory_allocation_error(). Also, we usually just allocate NFT_USERDATA_MAXLEN buffers.

Add a helper for that and use it.

---
 include/netlink.h       |  3 +++
 src/mnl.c               | 16 ++++------------
 src/netlink.c           |  7 ++-----
 src/netlink_linearize.c |  4 +---
 4 files changed, 10 insertions(+), 20 deletions(-)
include/netlink.h +0 -0
index 6766d7e8563f..15cbb332c8dd 100644

include/netlink.h struct nft_expr_loc *nft_expr_loc_find(const struct nftnl_expr *nle, · line 264

Quoted 5 lines
include/netlink.h +2 -0
@@ -260,4 +260,7 @@ struct nft_expr_loc *nft_expr_loc_find(const struct nftnl_expr *nle,
 
 struct dl_proto_ctx *dl_proto_ctx(struct rule_pp_ctx *ctx);
 
+#define _nftnl_udata_buf_alloc() \
+	memory_allocation_check(nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN))

Add a wrapper function, no macro.

#3 lore raw

Re: [PATCH nft 2/2] netlink: add and use _nftnl_udata_buf_alloc() helper

Quoted 15 lines · Pablo Neira Ayuso
Quoted 14 lines · Thomas Haller

We don't want to handle allocation errors, but crash via memory_allocation_error().
Also, we usually just allocate NFT_USERDATA_MAXLEN buffers.

Add a helper for that and use it.

---
 include/netlink.h       |  3 +++
 src/mnl.c               | 16 ++++------------
 src/netlink.c           |  7 ++-----
 src/netlink_linearize.c |  4 +---

 4 files changed, 10 insertions(+), 20 deletions(-)
Quoted 15 lines
Quoted 11 lines
include/netlink.h +0 -0
index 6766d7e8563f..15cbb332c8dd 100644
@@ -260,4 +260,7 @@ struct nft_expr_loc *nft_expr_loc_find(const
 struct nftnl_expr *nle,


 struct dl_proto_ctx *dl_proto_ctx(struct rule_pp_ctx *ctx);

include/netlink.h +2 -0
+#define _nftnl_udata_buf_alloc() \
+	memory_allocation_check(nftnl_udata_buf_alloc(NFT_USERDATA
Quoted 1 line

_MAXLEN))

Add a wrapper function, no macro.

Hi,

memory_allocation_error() is itself a macro, as it uses __FILE__,__LINE__

This is also a macro, to preserve those parameters.

Thomas

#4 lore raw

Re: [PATCH nft 2/2] netlink: add and use _nftnl_udata_buf_alloc() helper

Quoted 32 lines · Thomas Haller
Quoted 15 lines · Pablo Neira Ayuso
Quoted 14 lines · Thomas Haller

We don't want to handle allocation errors, but crash via memory_allocation_error().
Also, we usually just allocate NFT_USERDATA_MAXLEN buffers.

Add a helper for that and use it.

---
 include/netlink.h       |  3 +++
 src/mnl.c               | 16 ++++------------
 src/netlink.c           |  7 ++-----
 src/netlink_linearize.c |  4 +---

 4 files changed, 10 insertions(+), 20 deletions(-)
Quoted 15 lines
Quoted 11 lines
include/netlink.h +0 -0
index 6766d7e8563f..15cbb332c8dd 100644
@@ -260,4 +260,7 @@ struct nft_expr_loc *nft_expr_loc_find(const
 struct nftnl_expr *nle,


 struct dl_proto_ctx *dl_proto_ctx(struct rule_pp_ctx *ctx);

include/netlink.h +2 -0
+#define _nftnl_udata_buf_alloc() \
+	memory_allocation_check(nftnl_udata_buf_alloc(NFT_USERDATA
Quoted 1 line

_MAXLEN))

Add a wrapper function, no macro.

Quoted 4 lines

Hi,

memory_allocation_error() is itself a macro, as it uses __FILE__,__LINE__

In this case above, __FILE__ and __LINE__ does not provide much information?

nftnl_expr_alloc() returns NULL when support for an expression is missing in libnftnl, that provides a hint on that, this is very rare and it can only happen when developing support for new expressions.

Maybe simply say __func__ instead to know what function has failed when performing the memory allocation is a hint that is fine enough.

Quoted 4 lines

This is also a macro, to preserve those parameters.

Thomas

#5 lore raw

Re: [PATCH nft 2/2] netlink: add and use _nftnl_udata_buf_alloc() helper

Quoted 7 lines · Pablo Neira Ayuso
Quoted 6 lines · Thomas Haller
Quoted 3 lines · Pablo Neira Ayuso

Add a wrapper function, no macro.

Quoted 5 lines
Quoted 2 lines

memory_allocation_error() is itself a macro, as it uses __FILE__,__LINE__

In this case above, __FILE__ and __LINE__ does not provide much information?

In which case? The patch changes a repeated pattern to a macro(), without changing any behavior and without questioning the use of __FILE__:__LINE__.
.

Quoted 2 lines

nftnl_expr_alloc() returns NULL when support for an expression is missing in libnftnl,

The patch is not about nftnl_expr_alloc(). Do you mean nftnl_udata_buf_alloc()?

nftnl_udata_buf_alloc() fails exactly when malloc() fails. It's unrelated to missing "support for an expression".

Quoted 5 lines

that provides a hint on that, this is very rare
and it can only happen when developing support for new expressions.

Maybe simply say __func__ instead to know what function has failed when performing the memory allocation is a hint that is fine enough.

I wouldn't use __func__. It consumes more strings in the binary while providing less exact information.

Thomas

#6 lore raw

Re: [PATCH nft 2/2] netlink: add and use _nftnl_udata_buf_alloc() helper

Quoted 28 lines · Thomas Haller
Quoted 7 lines · Pablo Neira Ayuso
Quoted 6 lines · Thomas Haller
Quoted 3 lines · Pablo Neira Ayuso

Add a wrapper function, no macro.

Quoted 5 lines
Quoted 2 lines

memory_allocation_error() is itself a macro, as it uses __FILE__,__LINE__

In this case above, __FILE__ and __LINE__ does not provide much information?

In which case? The patch changes a repeated pattern to a macro(), without changing any behavior and without questioning the use of __FILE__:__LINE__.
.

Quoted 2 lines

nftnl_expr_alloc() returns NULL when support for an expression is missing in libnftnl,

The patch is not about nftnl_expr_alloc(). Do you mean nftnl_udata_buf_alloc()?

nftnl_udata_buf_alloc() fails exactly when malloc() fails. It's unrelated to missing "support for an expression".

Quoted 8 lines
Quoted 5 lines

that provides a hint on that, this is very rare
and it can only happen when developing support for new expressions.

Maybe simply say __func__ instead to know what function has failed when performing the memory allocation is a hint that is fine enough.

I wouldn't use __func__. It consumes more strings in the binary while providing less exact information.

OK, then if you prefer a generic OOM error message, that's also fine.

#7 lore raw
In reply to Thomas Haller
Quoted 25 lines · Thomas Haller

libnftables kills the process on out of memory (xmalloc()), so when we use libraries that propagate ENOMEM to libnftables, we also abort the process.

For example:

     nlr = nftnl_rule_alloc();
     if (!nlr)
          memory_allocation_error();

Add memory_allocation_check() macro which can simplify this common check to:

     nlr = memory_allocation_check(nftnl_rule_alloc());
---
 include/utils.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/utils.h +0 -0
index 36a28f893667..fcd7c598fe9f 100644

include/utils.h extern void __memory_allocation_error(const char *filename, uint32_t line) __nor · line 150

Quoted 9 lines
include/utils.h +6 -0
@@ -142,6 +142,16 @@ extern void __memory_allocation_error(const char *filename, uint32_t line) __nor
 #define memory_allocation_error()		\
 	__memory_allocation_error(__FILE__, __LINE__);
 
+#define memory_allocation_check(cmd)               \
+	({                                         \
+		typeof((cmd)) _v = (cmd);          \
+		const void *const _v2 = _v;        \
+                                                   \
+		if (!_v2)                          \

please don't hide a if branch inside a macro.

Quoted 10 lines
include/utils.h +4 -0
+			memory_allocation_error(); \
+		_v;                                \
+	})
+
 extern void xfree(const void *ptr);
 extern void *xmalloc(size_t size);
 extern void *xmalloc_array(size_t nmemb, size_t size);
Signature · 1 line
2.41.0
#8 lore raw
Quoted 10 lines · Pablo Neira Ayuso

include/utils.h extern void __memory_allocation_error(const char *filename, uint32_t line) __nor · line 150

Quoted 7 lines · Thomas Haller
include/utils.h +6 -0
+#define memory_allocation_check(cmd)               \
+	({                                         \
+		typeof((cmd)) _v = (cmd);          \
+		const void *const _v2 = _v;        \
+                                                   \
+		if (!_v2)                          \

please don't hide a if branch inside a macro.

What is the reason for this rule? Is there a style guide somewhere?

Quoted 4 lines

include/utils.h extern void __memory_allocation_error(const char *filename, uint32_t line) __nor · line 153

Quoted 4 lines
include/utils.h +3 -0
+			memory_allocation_error(); \
+		_v;                                \
+	})

It could be instead:

static inline void *__memory_allocation_check(const char *file, unsigned line, const void *ptr) {

    if (!ptr)
        __memory_allocation_error(file, line);
    return (void*) ptr;
}

#define memory_allocation_check(cmd) \
((typeof(cmd) __memory_allocation_check(__FILE__, __LINE__, (cmd))

Doesn't seem to make a difference either way.

Thomas

maintainer
#9 lore raw
Quoted 10 lines · Thomas Haller

static inline void *__memory_allocation_check(const char *file, unsigned line, const void *ptr) {

    if (!ptr)
        __memory_allocation_error(file, line);
    return (void*) ptr;
}
#define memory_allocation_check(cmd) \
   ((typeof(cmd) __memory_allocation_check(__FILE__, __LINE__, (cmd))

Doesn't seem to make a difference either way.

We seem to be moving in circles.

I suspect your agenda is to avoid repeating the existing

x = alloc()
if (!x)
barf()

pattern when adding userhandle support?

If so I think its best to just add a specific ubuf alloc wrapper that can't fail (i.e. like the 'xmalloc' wrappers).

Like Pablo said, I don't see any added value in providing FILE/LINE errors on stderr here. It could be as simple as exit().

#10 lore raw
Quoted 16 lines · Florian Westphal
Quoted 7 lines · Thomas Haller

static inline void *__memory_allocation_check(const char *file, unsigned line, const void *ptr) {
    if (!ptr)
        __memory_allocation_error(file, line);
    return (void*) ptr;
}

Quoted 5 lines

#define memory_allocation_check(cmd) \
   ((typeof(cmd) __memory_allocation_check(__FILE__, __LINE__, (cmd))

Doesn't seem to make a difference either way.

We seem to be moving in circles.

Quoted 7 lines

I suspect your agenda is to avoid repeating the existing

x = alloc()
if (!x)
  barf()

pattern when adding userhandle support?

exactly.

Quoted 3 lines

If so I think its best to just add a specific ubuf alloc wrapper that can't fail (i.e. like the 'xmalloc' wrappers).

I don't understand.

_nftnl_udata_buf_alloc() *is* that specific wrapper.

Maybe the name is bad... and such wrappers should have a "nft_x_" prefix (nft_x_nftnl_udata_buf_alloc()).

Quoted 4 lines

Like Pablo said, I don't see any added value in providing FILE/LINE errors on stderr here.  It could be as simple as exit().

I don't understand.

The two patches don't change anything about that. There is no change in behavior -- aside introducing a convenience wrapper for the repeated ENOMEM check.

If you want to change memory_allocation_error() to not use FILE/LINE or call exit(), then that is a separate discussion (I'd like to avoid).

Thomas