utils: add memory_allocation_check() helper
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
@@ -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
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
@@ -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
@@ -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
@@ -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
@@ -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
Pablo Neira Ayuso
maintainerRe: [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 allocateNFT_USERDATA_MAXLENbuffers.Add a helper for that and use it.
Signed-off-by Thomas Haller <[email protected]>--- 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
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.
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 allocateNFT_USERDATA_MAXLENbuffers.Add a helper for that and use it.
Signed-off-by Thomas Haller <[email protected]>---
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@@ -260,4 +260,7 @@ struct nft_expr_loc *nft_expr_loc_find(const struct nftnl_expr *nle,
structdl_proto_ctx*dl_proto_ctx(structrule_pp_ctx*ctx);include/netlink.h +2
-0+#define _nftnl_udata_buf_alloc () \ + memory_allocation_check(nftnl_udata_buf_alloc(NFT_USERDATAQuoted 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
Pablo Neira Ayuso
maintainerRe: [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 allocateNFT_USERDATA_MAXLENbuffers.Add a helper for that and use it.
Signed-off-by Thomas Haller <[email protected]>---
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@@ -260,4 +260,7 @@ struct nft_expr_loc *nft_expr_loc_find(const struct nftnl_expr *nle,
structdl_proto_ctx*dl_proto_ctx(structrule_pp_ctx*ctx);include/netlink.h +2
-0+#define _nftnl_udata_buf_alloc () \ + memory_allocation_check(nftnl_udata_buf_alloc(NFT_USERDATAQuoted 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
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
Pablo Neira Ayuso
maintainerRe: [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 meannftnl_udata_buf_alloc()?
nftnl_udata_buf_alloc()fails exactly whenmalloc()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.
Pablo Neira Ayuso
maintainerQuoted 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());Signed-off-by Thomas Haller <[email protected]>--- include/utils.h | 10 ++++++++++ 1 file changed, 10 insertions(+)include/utils.h +0
-0
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
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
Florian Westphal
maintainerQuoted 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().
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