ruleset: fix crash if we free sets included in the set_list
PATCH v2 1/1 ruleset: fix crash if we free sets included in the set_list
When we parse a ruleset which has a rule using a set. First step is parse the set, set up an id and add it to a set list. Later, we use this set list to find the set associated to the rule and we set up the set id to the expression (lookup expression) of the rule.
The problem is if we return this set using the function nft_ruleset_parse_file_cb and we free this set. We have a crash when we try to iterate in the set list.
This patch solves it, cloning the set and adding the new set to the set list.
---
[changes in v2]
* Added the function to clone set and set elems.
include/libnftnl/set.h | 2 ++ src/ruleset.c | 8 +++++++- src/set.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ src/set_elem.c | 27 +++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-)
include/libnftnl/set.h +2 -0
@@ -42,6 +42,7 @@ const void *nft_set_attr_get_data(struct nft_set *s, uint16_t attr, uint32_t *data_len);const char *nft_set_attr_get_str (struct nft_set *s,uint16_t attr);uint32_t nft_set_attr_get_u32 (struct nft_set *s,uint16_t attr); +struct nft_set *nft_set_clone (const struct nft_set *set);struct nlmsghdr ;
@@ -103,6 +104,7 @@ const char *nft_set_elem_attr_get_str(struct nft_set_elem *s, uint16_t attr);uint32_t nft_set_elem_attr_get_u32 (struct nft_set_elem *s,uint16_t attr);bool nft_set_elem_attr_is_set (const struct nft_set_elem *s,uint16_t attr); +struct nft_set_elem *nft_set_elem_clone (struct nft_set_elem *elem);#define nft_set_elem_nlmsg_build_hdr nft_nlmsg_build_hdrvoid nft_set_elems_nlmsg_build_payload (struct nlmsghdr *nlh,struct nft_set *s);
src/ruleset.c +7 -1
@@ -312,8 +312,14 @@ static int nft_ruleset_parse_set(struct nft_parse_ctx *ctx, struct nft_set *set, uint32_t type, struct nft_parse_err *err) { + struct nft_set *newset; +nft_set_attr_set_u32 (set,NFT_SET_ATTR_ID , ctx->set_id++); -nft_set_list_add_tail (set, ctx->set_list); + newset =nft_set_clone (set); +if (newset ==NULL ) + goto err; + +nft_set_list_add_tail (newset, ctx->set_list);nft_ruleset_ctx_set_u32 (ctx,NFT_RULESET_CTX_TYPE , type);nft_ruleset_ctx_set (ctx,NFT_RULESET_CTX_SET , set);
src/set.c +52 -0
58 lines not on this page · raw message
src/set_elem.c +27 -0
@@ -164,6 +164,33 @@ uint32_t nft_set_elem_attr_get_u32(struct nft_set_elem *s, uint16_t attr) }EXPORT_SYMBOL (nft_set_elem_attr_get_u32); +struct nft_set_elem *nft_set_elem_clone (struct nft_set_elem *elem) +{ +struct nft_set_elem *newelem; + + newelem =nft_set_elem_alloc (); +if (newelem ==NULL ) +return NULL ; + +if (elem->flags & (1 <<NFT_SET_ELEM_ATTR_FLAGS )) +nft_set_elem_attr_set_u32 (newelem,NFT_SET_ELEM_ATTR_FLAGS , + elem->set_elem_flags); +if (elem->flags & (1 <<NFT_SET_ELEM_ATTR_KEY )) +nft_set_elem_attr_set (newelem,NFT_SET_ELEM_ATTR_KEY , + elem->key.val, elem->key.len); +if (elem->flags & (1 <<NFT_SET_ELEM_ATTR_VERDICT )) +nft_set_elem_attr_set_u32 (newelem,NFT_SET_ELEM_ATTR_VERDICT , + elem->data.verdict); +if (elem->flags & (1 <<NFT_SET_ELEM_ATTR_CHAIN )) +nft_set_elem_attr_set_str (newelem,NFT_SET_ELEM_ATTR_CHAIN , + elem->data.chain); +if (elem->flags & (1 <<NFT_SET_ELEM_ATTR_DATA )) +nft_set_elem_attr_set (newelem,NFT_SET_ELEM_ATTR_DATA , + elem->data.val, elem->data.len); + +return newelem; +} +void nft_set_elem_nlmsg_build_payload (struct nlmsghdr *nlh,struct nft_set_elem *e) {
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerQuoted 69 lines · Alvaro Neira Ayuso
When we parse a ruleset which has a rule using a set. First step is parse the set, set up an id and add it to a set list. Later, we use this set list to find the set associated to the rule and we set up the set id to the expression (lookup expression) of the rule.
The problem is if we return this set using the function
nft_ruleset_parse_file_cband we free this set. We have a crash when we try to iterate in the set list.This patch solves it, cloning the set and adding the new set to the set list.
Signed-off-by Alvaro Neira Ayuso <[email protected]>---
[changes in v2]
* Added the function to clone set and set elems.include/libnftnl/set.h | 2 ++ src/ruleset.c | 8 +++++++- src/set.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ src/set_elem.c | 27 +++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-)include/libnftnl/set.h +2
-0@@ -42,6 +42,7 @@ const void *nft_set_attr_get_data(struct nft_set *s, uint16_t attr, uint32_t *data_len);const char *nft_set_attr_get_str (struct nft_set *s,uint16_t attr);uint32_t nft_set_attr_get_u32 (struct nft_set *s,uint16_t attr); +struct nft_set *nft_set_clone (const struct nft_set *set);struct nlmsghdr ;@@ -103,6 +104,7 @@ const char *nft_set_elem_attr_get_str(struct nft_set_elem *s, uint16_t attr);uint32_t nft_set_elem_attr_get_u32 (struct nft_set_elem *s,uint16_t attr);bool nft_set_elem_attr_is_set (const struct nft_set_elem *s,uint16_t attr); +struct nft_set_elem *nft_set_elem_clone (struct nft_set_elem *elem);#define nft_set_elem_nlmsg_build_hdr nft_nlmsg_build_hdrvoid nft_set_elems_nlmsg_build_payload (struct nlmsghdr *nlh,struct nft_set *s);src/ruleset.c +7
-1@@ -312,8 +312,14 @@ static int nft_ruleset_parse_set(struct nft_parse_ctx *ctx, struct nft_set *set, uint32_t type, struct nft_parse_err *err) { + struct nft_set *newset; +nft_set_attr_set_u32 (set,NFT_SET_ATTR_ID , ctx->set_id++); -nft_set_list_add_tail (set, ctx->set_list); + newset =nft_set_clone (set); +if (newset ==NULL ) + goto err; + +nft_set_list_add_tail (newset, ctx->set_list);nft_ruleset_ctx_set_u32 (ctx,NFT_RULESET_CTX_TYPE , type);nft_ruleset_ctx_set (ctx,NFT_RULESET_CTX_SET , set);src/set.c +0
-0@@ -249,6 +249,58 @@ uint32_t nft_set_attr_get_u32(struct nft_set *s, uint16_t attr) }EXPORT_SYMBOL (nft_set_attr_get_u32);
src/set.c uint32_t nft_set_attr_get_u32(struct nft_set *s, uint16_t attr) · line 260
Quoted 10 lines
src/set.c +9
-0+struct nft_set *nft_set_clone (const struct nft_set *set) +{ +struct nft_set *newset; +struct nft_set_elem *elem, *tmp, *newelem; + + newset =nft_set_alloc (); +if (newset ==NULL ) +return NULL ; +
You better memcmpy() to save these many LOCs...
Quoted 28 lines
src/set.c +28
-0+if (set->flags & (1 <<NFT_SET_ATTR_TABLE )) +nft_set_attr_set_str (newset,NFT_SET_ATTR_TABLE , set->table); +if (set->flags & (1 <<NFT_SET_ATTR_NAME )) +nft_set_attr_set_str (newset,NFT_SET_ATTR_NAME , set->name); +if (set->flags & (1 <<NFT_SET_ATTR_FLAGS )) +nft_set_attr_set_u32 (newset,NFT_SET_ATTR_FLAGS , + set->set_flags); +if (set->flags & (1 <<NFT_SET_ATTR_KEY_TYPE )) +nft_set_attr_set_u32 (newset,NFT_SET_ATTR_KEY_TYPE , + set->key_type); +if (set->flags & (1 <<NFT_SET_ATTR_KEY_LEN )) +nft_set_attr_set_u32 (newset,NFT_SET_ATTR_KEY_LEN , + set->key_len); +if (set->flags & (1 <<NFT_SET_ATTR_DATA_TYPE )) +nft_set_attr_set_u32 (newset,NFT_SET_ATTR_DATA_TYPE , + set->data_type); +if (set->flags & (1 <<NFT_SET_ATTR_DATA_LEN )) +nft_set_attr_set_u32 (newset,NFT_SET_ATTR_DATA_LEN , + set->data_len); +if (set->flags & (1 <<NFT_SET_ATTR_FAMILY )) +nft_set_attr_set_u32 (newset,NFT_SET_ATTR_FAMILY , set->family); +if (set->flags & (1 <<NFT_SET_ATTR_ID )) +nft_set_attr_set_u32 (newset,NFT_SET_ATTR_ID , set->id); +if (set->flags & (1 <<NFT_SET_ATTR_POLICY )) +nft_set_attr_set_u32 (newset,NFT_SET_ATTR_POLICY , set->policy); +if (set->flags & (1 <<NFT_SET_ATTR_DESC_SIZE )) +nft_set_attr_set_u32 (newset,NFT_SET_ATTR_DESC_SIZE , + set->desc.size);
Pablo Neira Ayuso
maintainerQuoted 83 lines · Pablo Neira Ayuso
Quoted 69 lines · Alvaro Neira Ayuso
When we parse a ruleset which has a rule using a set. First step is parse the set, set up an id and add it to a set list. Later, we use this set list to find the set associated to the rule and we set up the set id to the expression (lookup expression) of the rule.
The problem is if we return this set using the function
nft_ruleset_parse_file_cband we free this set. We have a crash when we try to iterate in the set list.This patch solves it, cloning the set and adding the new set to the set list.
Signed-off-by Alvaro Neira Ayuso <[email protected]>---
[changes in v2]
* Added the function to clone set and set elems.include/libnftnl/set.h | 2 ++ src/ruleset.c | 8 +++++++- src/set.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ src/set_elem.c | 27 +++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-)include/libnftnl/set.h +2
-0@@ -42,6 +42,7 @@ const void *nft_set_attr_get_data(struct nft_set *s, uint16_t attr, uint32_t *data_len);const char *nft_set_attr_get_str (struct nft_set *s,uint16_t attr);uint32_t nft_set_attr_get_u32 (struct nft_set *s,uint16_t attr); +struct nft_set *nft_set_clone (const struct nft_set *set);struct nlmsghdr ;@@ -103,6 +104,7 @@ const char *nft_set_elem_attr_get_str(struct nft_set_elem *s, uint16_t attr);uint32_t nft_set_elem_attr_get_u32 (struct nft_set_elem *s,uint16_t attr);bool nft_set_elem_attr_is_set (const struct nft_set_elem *s,uint16_t attr); +struct nft_set_elem *nft_set_elem_clone (struct nft_set_elem *elem);#define nft_set_elem_nlmsg_build_hdr nft_nlmsg_build_hdrvoid nft_set_elems_nlmsg_build_payload (struct nlmsghdr *nlh,struct nft_set *s);src/ruleset.c +7
-1@@ -312,8 +312,14 @@ static int nft_ruleset_parse_set(struct nft_parse_ctx *ctx, struct nft_set *set, uint32_t type, struct nft_parse_err *err) { + struct nft_set *newset; +nft_set_attr_set_u32 (set,NFT_SET_ATTR_ID , ctx->set_id++); -nft_set_list_add_tail (set, ctx->set_list); + newset =nft_set_clone (set); +if (newset ==NULL ) + goto err; + +nft_set_list_add_tail (newset, ctx->set_list);nft_ruleset_ctx_set_u32 (ctx,NFT_RULESET_CTX_TYPE , type);nft_ruleset_ctx_set (ctx,NFT_RULESET_CTX_SET , set);src/set.c +0
-0@@ -249,6 +249,58 @@ uint32_t nft_set_attr_get_u32(struct nft_set *s, uint16_t attr) }EXPORT_SYMBOL (nft_set_attr_get_u32);src/set.c uint32_t nft_set_attr_get_u32(struct nft_set *s, uint16_t attr) · line 260
Quoted 10 lines
src/set.c +9
-0+struct nft_set *nft_set_clone (const struct nft_set *set) +{ +struct nft_set *newset; +struct nft_set_elem *elem, *tmp, *newelem; + + newset =nft_set_alloc (); +if (newset ==NULL ) +return NULL ; +You better
memcmpy()to save these many LOCs...
Quoted 4 lines
src/set.c uint32_t nft_set_attr_get_u32(struct nft_set *s, uint16_t attr) · line 264
Quoted 4 lines
src/set.c +4
-0+if (set->flags & (1 <<NFT_SET_ATTR_TABLE )) +nft_set_attr_set_str (newset,NFT_SET_ATTR_TABLE , set->table); +if (set->flags & (1 <<NFT_SET_ATTR_NAME )) +nft_set_attr_set_str (newset,NFT_SET_ATTR_NAME , set->name);
Wait, you can memcpy of course. But make sure you clone fields that are pointers too, otherwise both objects will pointer to the same table and name. This will result in a crash if one of the objects is releases and the second will have invalid references to released memory.
Quoted 84 lines · Pablo Neira Ayuso
Quoted 83 lines · Pablo Neira Ayuso
Quoted 69 lines · Alvaro Neira Ayuso
When we parse a ruleset which has a rule using a set. First step is parse the set, set up an id and add it to a set list. Later, we use this set list to find the set associated to the rule and we set up the set id to the expression (lookup expression) of the rule.
The problem is if we return this set using the function
nft_ruleset_parse_file_cband we free this set. We have a crash when we try to iterate in the set list.This patch solves it, cloning the set and adding the new set to the set list.
Signed-off-by Alvaro Neira Ayuso <[email protected]>---
[changes in v2]
* Added the function to clone set and set elems.include/libnftnl/set.h | 2 ++ src/ruleset.c | 8 +++++++- src/set.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ src/set_elem.c | 27 +++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-)include/libnftnl/set.h +2
-0@@ -42,6 +42,7 @@ const void *nft_set_attr_get_data(struct nft_set *s, uint16_t attr, uint32_t *data_len);const char *nft_set_attr_get_str (struct nft_set *s,uint16_t attr);uint32_t nft_set_attr_get_u32 (struct nft_set *s,uint16_t attr); +struct nft_set *nft_set_clone (const struct nft_set *set);struct nlmsghdr ;@@ -103,6 +104,7 @@ const char *nft_set_elem_attr_get_str(struct nft_set_elem *s, uint16_t attr);uint32_t nft_set_elem_attr_get_u32 (struct nft_set_elem *s,uint16_t attr);bool nft_set_elem_attr_is_set (const struct nft_set_elem *s,uint16_t attr); +struct nft_set_elem *nft_set_elem_clone (struct nft_set_elem *elem);#define nft_set_elem_nlmsg_build_hdr nft_nlmsg_build_hdrvoid nft_set_elems_nlmsg_build_payload (struct nlmsghdr *nlh,struct nft_set *s);src/ruleset.c +7
-1@@ -312,8 +312,14 @@ static int nft_ruleset_parse_set(struct nft_parse_ctx *ctx, struct nft_set *set, uint32_t type, struct nft_parse_err *err) { + struct nft_set *newset; +nft_set_attr_set_u32 (set,NFT_SET_ATTR_ID , ctx->set_id++); -nft_set_list_add_tail (set, ctx->set_list); + newset =nft_set_clone (set); +if (newset ==NULL ) + goto err; + +nft_set_list_add_tail (newset, ctx->set_list);nft_ruleset_ctx_set_u32 (ctx,NFT_RULESET_CTX_TYPE , type);nft_ruleset_ctx_set (ctx,NFT_RULESET_CTX_SET , set);src/set.c +0
-0@@ -249,6 +249,58 @@ uint32_t nft_set_attr_get_u32(struct nft_set *s, uint16_t attr) }EXPORT_SYMBOL (nft_set_attr_get_u32);src/set.c uint32_t nft_set_attr_get_u32(struct nft_set *s, uint16_t attr) · line 260
Quoted 10 lines
src/set.c +9
-0+struct nft_set *nft_set_clone (const struct nft_set *set) +{ +struct nft_set *newset; +struct nft_set_elem *elem, *tmp, *newelem; + + newset =nft_set_alloc (); +if (newset ==NULL ) +return NULL ; +You better
memcmpy()to save these many LOCs...
Quoted 10 lines
Quoted 4 lines
src/set.c uint32_t nft_set_attr_get_u32(struct nft_set *s, uint16_t attr) · line 264
Quoted 4 lines
src/set.c +4
-0+if (set->flags & (1 <<NFT_SET_ATTR_TABLE )) +nft_set_attr_set_str (newset,NFT_SET_ATTR_TABLE , set->table); +if (set->flags & (1 <<NFT_SET_ATTR_NAME )) +nft_set_attr_set_str (newset,NFT_SET_ATTR_NAME , set->name);Wait, you can memcpy of course. But make sure you clone fields that are pointers too, otherwise both objects will pointer to the same table and name. This will result in a crash if one of the objects is releases and the second will have invalid references to released memory.
Ok, I'm going to take a look and rework the patch using memcpy. --
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html