Cache update fix && intra-transaction rule references
Phil Sutter
reviewerPATCH v4 4/7 src: Restore local entries after cache update
In reply to Phil SutterWhen batching up multiple commands, one may run into a situation where the current command requires a cache update while the previous ones didn't and that causes objects added by previous commands to be removed from cache. If the current or any following command references any of these objects, the command is rejected.
Resolve this by copying Florian's solution from iptables-nft: After droping the whole cache and populating it again with entries fetched from kernel, use the current list of commands to restore local entries again.
---
Changes since v2:
- Move cache_add_commands() and called helper functions to a better place, it calls cache_is_complete().
- Add error handling to cache_add_commands() and helper functions. This should cover the case where required ruleset parts have disappeared from kernel. Error reporting makes use of {table,chain}_not_found(), so prepare a struct eval_ctx which also serves fine for passing pointers around.
- Drop cache bug workaround in tests/json_echo.
Changes since v1:
- Don't add anonymous sets to cache when restoring, as suggested by Eric Garver.
--- src/rule.c | 83 ++++++++++++++++++++++++++++++++++++- tests/json_echo/run-test.py | 6 +-- 2 files changed, 84 insertions(+), 5 deletions(-)
src/rule.c +82 -1
@@ -242,6 +242,87 @@ static bool cache_is_updated(struct nft_cache *cache, uint16_t genid)return genid && genid == cache->genid; } +static int cache_add_table_cmd (struct eval_ctx *ectx) +{ +struct table *table; + +if (table_lookup (&ectx->cmd->handle, &ectx->nft->cache)) +return 0 ; + +if (!ectx->cmd->table) { + table =table_alloc (); +handle_merge (&table->handle, &ectx->cmd->handle); +table_add_hash (table, &ectx->nft->cache); + }else { +table_add_hash (table_get (ectx->cmd->table), &ectx->nft->cache); + } +return 0 ; +} + +static int cache_add_chain_cmd (struct eval_ctx *ectx) +{ +struct table *table; +struct chain *chain; + + table =table_lookup (&ectx->cmd->handle, &ectx->nft->cache); +if (!table) +return table_not_found (ectx); + +if (!ectx->cmd->chain) { +if (!chain_lookup (table, &ectx->cmd->handle)) { + chain =chain_alloc (NULL ); +handle_merge (&chain->handle, &ectx->cmd->handle); +chain_add_hash (chain, table); + } + }else if (!chain_lookup (table, &ectx->cmd->chain->handle)) { +chain_add_hash (chain_get (ectx->cmd->chain), table); + } +return 0 ; +} + +static int cache_add_set_cmd (struct eval_ctx *ectx) +{ +struct table *table; + + table =table_lookup (&ectx->cmd->handle, &ectx->nft->cache); +if (!table) +return table_not_found (ectx); + +if (!set_lookup (table, ectx->cmd->set->handle.set.name)) +set_add_hash (set_get (ectx->cmd->set), table); +return 0 ; +} + +static int cache_add_commands (struct nft_ctx *nft,struct list_head *msgs) +{ +struct eval_ctx ectx = { + .nft = nft, + .msgs = msgs, + }; +int ret =0 ; + +list_for_each_entry (ectx.cmd, &nft->cmds, list) { +switch (ectx.cmd->obj) { +case CMD_OBJ_TABLE : + ret =cache_add_table_cmd (&ectx); +break ; +case CMD_OBJ_CHAIN : + ret =cache_add_chain_cmd (&ectx); +break ; +case CMD_OBJ_SET : +if (ectx.cmd->set->flags &NFT_SET_ANONYMOUS ) +continue ; + ret =cache_add_set_cmd (&ectx); +break ; +default : +break ; + } +if (ret) +break ; + } +return ret; +} +int cache_update (struct nft_ctx *nft,enum cmd_ops cmd,struct list_head *msgs) {uint16_t genid;
@@ -275,7 +356,7 @@ replay: } cache->genid = genid; cache->cmd = cmd; -return 0 ; +return cache_add_commands (nft, msgs); }static void __cache_flush (struct list_head *table_list)
tests/json_echo/run-test.py +2 -4
@@ -270,12 +270,10 @@ add_quota["add"]["quota"]["name"] = "q" do_flush() print "doing multi add" -# XXX: Add table separately, otherwise this triggers cache bug -out = do_command(add_table) -thandle = get_handle(out, add_table["add"]) -add_multi = [ add_chain, add_set, add_rule ] +add_multi = [ add_table, add_chain, add_set, add_rule ] out = do_command(add_multi) +thandle = get_handle(out, add_table["add"]) chandle = get_handle(out, add_chain["add"]) shandle = get_handle(out, add_set["add"]) rhandle = get_handle(out, add_rule["add"])
Signature · 1 line
2.21.0
Phil Sutter
reviewerPATCH v4 7/7 src: Support intra-transaction rule references
In reply to Phil SutterA rule may be added before or after another one using index keyword. To support for the other rule being added within the same batch, one has to make use of NFTNL_RULE_ID and NFTNL_RULE_POSITION_ID attributes. This patch does just that among a few more crucial things:
* Fetch full kernel ruleset upon encountering a rule which references another one. Any earlier rule add/insert commands are then restored by cache_add_commands().
* Avoid cache updates for rules not referencing another one, but make sure cache is not treated as complete afterwards so a later rule with reference will cause cache update and repopulation from command list.
* Reduce rule_translate_index() to its core code which is the actual linking of rules and consequently rename the function. The removed bits are pulled into the calling rule_evaluate() to reduce code duplication in between cache inserts with and without rule reference.
* Pass the current command op to rule_evaluate() as indicator whether to insert before or after a referenced rule or at beginning or end of chain in cache. Exploit this from chain_evaluate() to avoid adding the chain's rules a second time.
Light casts shadow though: It has been possible to reference another rule of the same transaction via its *guessed* handle - this patch removes that possibility.
---
Changes since v1:
- Move rule list manipulation into a dedicated function rule_cache_update().
- Restore old performance for simple commands by fetching a full rule cache only if the currently evaluated rule references another one.
- Extend 0024rule_0 test a bit to make sure things work with interactive nft also.
--- include/rule.h | 6 + src/evaluate.c | 103 +++++++++++------- src/mnl.c | 4 + src/rule.c | 53 +++++++++ .../shell/testcases/cache/0003_cache_update_0 | 7 ++ .../shell/testcases/nft-f/0006action_object_0 | 2 +- tests/shell/testcases/transactions/0024rule_0 | 17 +++ .../transactions/dumps/0024rule_0.nft | 8 ++ 8 files changed, 162 insertions(+), 38 deletions(-) create mode 100755 tests/shell/testcases/transactions/0024rule_0 create mode 100644 tests/shell/testcases/transactions/dumps/0024rule_0.nft
include/rule.h +6 -0
@@ -73,6 +73,8 @@ struct handle {struct position_spec position;struct position_spec index;uint32_t set_id; +uint32_t rule_id; +uint32_t position_id; };extern void handle_merge (struct handle *dst,const struct handle *src);
@@ -263,6 +265,10 @@ extern struct rule *rule_lookup(const struct chain *chain, uint64_t handle);extern struct rule *rule_lookup_by_index (const struct chain *chain,uint64_t index); +enum cmd_ops ; +extern void rule_cache_update (enum cmd_ops op,struct chain *chain, +struct rule *rule,struct rule *ref); +/** * struct set - nftables set *
src/evaluate.c +66 -37
136 lines not on this page · raw message
src/mnl.c +4 -0
@@ -327,6 +327,10 @@ int mnl_nft_rule_add(struct netlink_ctx *ctx, const struct cmd *cmd,nftnl_rule_set_str (nlr,NFTNL_RULE_CHAIN , h->chain.name);if (h->position.id)nftnl_rule_set_u64 (nlr,NFTNL_RULE_POSITION , h->position.id); +if (h->rule_id) +nftnl_rule_set_u32 (nlr,NFTNL_RULE_ID , h->rule_id); +if (h->position_id) +nftnl_rule_set_u32 (nlr,NFTNL_RULE_POSITION_ID , h->position_id);netlink_linearize_rule (ctx, nlr, rule); nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch),
src/rule.c +53 -0
71 lines not on this page · raw message
tests/shell/testcases/cache/0003_cache_update_0 +7 -0
@@ -34,6 +34,9 @@ EOF # add rule ip t4 c meta l4proto icmp accept -> rule to reference in next step # add rule ip t4 c index 0 drop -> index 0 is not found due to rule cache not # being updated +# add rule ip t4 c index 2 drop -> index 2 is not found due to igmp rule being +# in same transaction and therefore not having +# an allocated handle $NFT -i >/dev/null <<EOF add table ip t4; add chain ip t4 c add rule ip t4 c meta l4proto icmp accept
@@ -41,3 +44,7 @@ EOF $NFT -f - >/dev/null <<EOF add rule ip t4 c index 0 drop EOF +$NFT -f - >/dev/null <<EOF +add rule ip t4 c meta l4proto igmp accept +add rule ip t4 c index 2 drop +EOF
tests/shell/testcases/nft-f/0006action_object_0 +1 -1
@@ -16,7 +16,6 @@ generate1() add set $family t s {type inet_service;} add element $family t s {8080} insert rule $family t c meta l4proto tcp tcp dport @s accept - replace rule $family t c handle 2 meta l4proto tcp tcp dport {9090} add map $family t m {type inet_service:verdict;} add element $family t m {10080:drop} insert rule $family t c meta l4proto tcp tcp dport vmap @m
@@ -28,6 +27,7 @@ generate2() { local family=$1 echo " + replace rule $family t c handle 2 meta l4proto tcp tcp dport {9090} flush chain $family t c delete element $family t m {10080:drop} delete element $family t s {8080}
tests/shell/testcases/transactions/0024rule_0 new file +17 -0
@@ -0,0 +1,17 @@ +#!/bin/bash + +RULESET="flush ruleset +add table x +add chain x y +add rule x y accept comment rule1 +add rule x y accept comment rule4 +add rule x y index 0 accept comment rule2 +insert rule x y index 2 accept comment rule3" + +$NFT -f - <<< "$RULESET" && \ + $NFT -f - <<< "$RULESET" && \ + echo "$RULESET" | tr '\n' ';' | $NFT -i >/dev/null && \ + exit 0 +echo "E: intra-transaction rule reference failed" +exit 1 +
tests/shell/testcases/transactions/dumps/0024rule_0.nft new file +8 -0
@@ -0,0 +1,8 @@ +table ip x { + chain y { + accept comment "rule1" + accept comment "rule2" + accept comment "rule3" + accept comment "rule4" + } +}
Signature · 1 line
2.21.0
Phil Sutter
reviewerPATCH v4 1/7 src: Fix cache_flush() in cache_needs_more() logic
In reply to Phil SutterCommit 34a20645d54fa enabled cache updates depending on command causing it. As a side-effect, this disabled measures in cache_flush() preventing a later cache update. Re-establish this by setting cache->cmd in addition to cache->genid after dropping cache entries.
While being at it, set cache->cmd in cache_release() as well. This shouldn't be necessary since zeroing cache->genid should suffice for cache_update(), but better be consistent (and future-proof) here.
---
Changes since v1:
- Adjust cache_release() as well, just to make sure.
--- src/rule.c | 3 +++ 1 file changed, 3 insertions(+)
src/rule.c +3 -0
@@ -299,12 +299,15 @@ void cache_flush(struct nft_ctx *nft, enum cmd_ops cmd, struct list_head *msgs)__cache_flush (&cache->list); cache->genid =mnl_genid_get (&ctx); + cache->cmd =CMD_LIST ; }void cache_release (struct nft_cache *cache) {__cache_flush (&cache->list); cache->genid =0 ; + cache->cmd =CMD_INVALID ; + }/* internal ID to uniquely identify a set in the batch */
Signature · 1 line
2.21.0
Phil Sutter
reviewerPATCH v4 5/7 rule: Introduce rule_lookup_by_index()
In reply to Phil SutterIn contrast to rule_lookup(), this function returns a chain's rule at a given index instead of by handle.
--- include/rule.h | 2 ++ src/rule.c | 11 +++++++++++ 2 files changed, 13 insertions(+)
include/rule.h +2 -0
@@ -260,6 +260,8 @@ extern struct rule *rule_get(struct rule *rule);extern void rule_free (struct rule *rule);extern void rule_print (const struct rule *rule,struct output_ctx *octx);extern struct rule *rule_lookup (const struct chain *chain,uint64_t handle); +extern struct rule *rule_lookup_by_index (const struct chain *chain, +uint64_t index);/** * struct set - nftables set
src/rule.c +11 -0
@@ -716,6 +716,17 @@ struct rule *rule_lookup(const struct chain *chain, uint64_t handle)return NULL ; } +struct rule *rule_lookup_by_index (const struct chain *chain,uint64_t index) +{ +struct rule *rule; + +list_for_each_entry (rule, &chain->rules, list) { +if (!--index) +return rule; + } +return NULL ; +} +struct scope *scope_init (struct scope *scope,const struct scope *parent) { scope->parent = parent;
Signature · 1 line
2.21.0
Phil Sutter
reviewerPATCH v4 2/7 libnftables: Keep list of commands in nft context
In reply to Phil SutterTo fix the pending issues with cache updates, the list of commands needs to be accessible from within cache_update(). In theory, there is a path via nft->state->cmds but that struct parser_state is used (and initialized) by bison parser only so that does not work with JSON parser.
--- include/nftables.h | 1 + src/libnftables.c | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 11 deletions(-)
include/nftables.h +1 -0
@@ -102,6 +102,7 @@ struct nft_ctx {struct parser_state *state;void *scanner;void *json_root; +struct list_head cmds;FILE *f[MAX_INCLUDE_DEPTH ]; };
src/libnftables.c +10 -11
@@ -143,6 +143,7 @@ struct nft_ctx *nft_ctx_new(uint32_t flags)nft_ctx_add_include_path (ctx,DEFAULT_INCLUDE_PATH ); ctx->parser_max_errors =10 ;init_list_head (&ctx->cache.list); +init_list_head (&ctx->cmds); ctx->flags = flags; ctx->output.output_fp = stdout; ctx->output.error_fp = stderr;
@@ -342,7 +343,7 @@ static int nft_parse_bison_buffer(struct nft_ctx *nft, const char *buf,struct cmd *cmd;int ret; -parser_init (nft, nft->state, msgs, cmds); +parser_init (nft, nft->state, msgs, &nft->cmds); nft->scanner =scanner_init (nft->state);scanner_push_buffer (nft->scanner, &indesc_cmdline, buf);
@@ -381,7 +382,6 @@ int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf) {struct cmd *cmd, *next;LIST_HEAD (msgs); -LIST_HEAD (cmds);char *nlbuf;int rc = -EINVAL ;
@@ -389,17 +389,17 @@ int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf)sprintf (nlbuf,"%s\n" , buf);if (nft_output_json (&nft->output)) - rc =nft_parse_json_buffer (nft, nlbuf, &msgs, &cmds); + rc =nft_parse_json_buffer (nft, nlbuf, &msgs, &nft->cmds);if (rc == -EINVAL ) - rc =nft_parse_bison_buffer (nft, nlbuf, &msgs, &cmds); + rc =nft_parse_bison_buffer (nft, nlbuf, &msgs, &nft->cmds);if (rc) goto err; -if (nft_netlink (nft, &cmds, &msgs, nft->nf_sock) !=0 ) +if (nft_netlink (nft, &nft->cmds, &msgs, nft->nf_sock) !=0 ) rc =-1 ; err:erec_print_list (&nft->output, &msgs, nft->debug_mask); -list_for_each_entry_safe (cmd, next, &cmds, list) { +list_for_each_entry_safe (cmd, next, &nft->cmds, list) {list_del (&cmd->list);cmd_free (cmd); }
@@ -421,7 +421,6 @@ int nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename) {struct cmd *cmd, *next;LIST_HEAD (msgs); -LIST_HEAD (cmds);int rc; rc =cache_update (nft,CMD_INVALID , &msgs);
@@ -433,17 +432,17 @@ int nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename) rc = -EINVAL ;if (nft_output_json (&nft->output)) - rc =nft_parse_json_filename (nft, filename, &msgs, &cmds); + rc =nft_parse_json_filename (nft, filename, &msgs, &nft->cmds);if (rc == -EINVAL ) - rc =nft_parse_bison_filename (nft, filename, &msgs, &cmds); + rc =nft_parse_bison_filename (nft, filename, &msgs, &nft->cmds);if (rc) goto err; -if (nft_netlink (nft, &cmds, &msgs, nft->nf_sock) !=0 ) +if (nft_netlink (nft, &nft->cmds, &msgs, nft->nf_sock) !=0 ) rc =-1 ; err:erec_print_list (&nft->output, &msgs, nft->debug_mask); -list_for_each_entry_safe (cmd, next, &cmds, list) { +list_for_each_entry_safe (cmd, next, &nft->cmds, list) {list_del (&cmd->list);cmd_free (cmd); }
Signature · 1 line
2.21.0
Phil Sutter
reviewerPATCH v4 3/7 src: Make {table,chain}_not_found() public
In reply to Phil Sutter--- include/rule.h | 3 +++ src/evaluate.c | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-)
include/rule.h +3 -0
@@ -646,4 +646,7 @@ struct timeout_protocol {extern struct timeout_protocol timeout_protocol[IPPROTO_MAX ];extern int timeout_str2num (uint16_t l4proto,struct timeout_state *ts); +int table_not_found (struct eval_ctx *ctx); +int chain_not_found (struct eval_ctx *ctx); +#endif /* NFTABLES_RULE_H */
src/evaluate.c +2 -2
@@ -166,7 +166,7 @@ static struct table *table_lookup_global(struct eval_ctx *ctx)return table; } -static int table_not_found (struct eval_ctx *ctx) +int table_not_found (struct eval_ctx *ctx) {struct table *table;
@@ -181,7 +181,7 @@ static int table_not_found(struct eval_ctx *ctx) family2str(table->handle.family)); } -static int chain_not_found (struct eval_ctx *ctx) +int chain_not_found (struct eval_ctx *ctx) {const struct table *table;struct chain *chain;
Signature · 1 line
2.21.0
Phil Sutter
reviewerPATCH v4 6/7 src: Make cache_is_complete() public
In reply to Phil Sutter--- include/rule.h | 1 + src/rule.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
include/rule.h +1 -0
@@ -638,6 +638,7 @@ extern int cache_update(struct nft_ctx *ctx, enum cmd_ops cmd,extern void cache_flush (struct nft_ctx *ctx,enum cmd_ops cmd,struct list_head *msgs);extern void cache_release (struct nft_cache *cache); +extern bool cache_is_complete (struct nft_cache *cache,enum cmd_ops cmd);struct timeout_protocol {uint32_t array_size;
src/rule.c +1 -1
@@ -232,7 +232,7 @@ static int cache_completeness(enum cmd_ops cmd)return 1 ; } -static bool cache_is_complete (struct nft_cache *cache,enum cmd_ops cmd) +bool cache_is_complete (struct nft_cache *cache,enum cmd_ops cmd) {return cache_completeness (cache->cmd) >=cache_completeness (cmd); }
Signature · 1 line
2.21.0
Phil Sutter
reviewerv4 cover Cache update fix && intra-transaction rule references
This series combines the two series submitted earlier since they became closely related in this iteration.
Patch 1 fixes a basic problem with cache_flush() after Eric's cache_needs_more() change.
Patches 2, 3, 5 and 6 are requirements for patches 4 and 7 which are the interesting ones: Patch 4 restores needed cache entries from command list after a cache update. Patch 7 enables referencing a rule added by the same transaction from another new rule by further exploiting the logic added by patch 4.
Changes since v2 of "Resolve cache update woes" and v1 of "Support intra-transaction rule references":
- Adjust cache_release() just like cache_flush().
- Split preparation work into separate patches.
- Adjust cache_add_commands() for later reuse by rule reference code, also add error handling in case kernel ruleset changes incompatibly.
- Finally drop that workaround in tests/json_echo.
- Introduce rule_cache_update() as requested.
- Avoid fetching a full cache if the new rule does not contain any reference.
Phil Sutter (7):
src: Fix cache_flush() in cache_needs_more() logic
libnftables: Keep list of commands in nft context
src: Make {table,chain}_not_found() public
src: Restore local entries after cache update
rule: Introduce rule_lookup_by_index()
src: Make cache_is_complete() public
src: Support intra-transaction rule references
include/nftables.h | 1 + include/rule.h | 12 ++ src/evaluate.c | 107 +++++++----- src/libnftables.c | 21 ++- src/mnl.c | 4 + src/rule.c | 152 +++++++++++++++++- tests/json_echo/run-test.py | 6 +- .../shell/testcases/cache/0003_cache_update_0 | 7 + .../shell/testcases/nft-f/0006action_object_0 | 2 +- tests/shell/testcases/transactions/0024rule_0 | 17 ++ .../transactions/dumps/0024rule_0.nft | 8 + 11 files changed, 280 insertions(+), 57 deletions(-) create mode 100755 tests/shell/testcases/transactions/0024rule_0 create mode 100644 tests/shell/testcases/transactions/dumps/0024rule_0.nft
Signature · 1 line
2.21.0
Re: [nft PATCH v4 1/7] src: Fix cache_flush() in cache_needs_more() logic
In reply to Phil SutterQuoted 1 line · Phil Sutter
Commit
34a20645d54faenabled cache updates depending on command causing
Actual hash is eeda228c2d17.
Quoted 9 lines
it. As a side-effect, this disabled measures in
cache_flush()preventing a later cache update. Re-establish this by settingcache->cmdin addition tocache->genidafter dropping cache entries.While being at it, set
cache->cmdincache_release()as well. This shouldn't be necessary since zeroingcache->genidshould suffice forcache_update(), but better be consistent (and future-proof) here.Fixes 34a20645d54fa ("src: update cache if cmd is more specific")
Actual hash is eeda228c2d17.
Quoted 30 lines
Signed-off-by Phil Sutter <[email protected]>---
Changes since v1:
- Adjustcache_release()as well, just to make sure.--- src/rule.c | 3 +++ 1 file changed, 3 insertions(+)src/rule.c +3
-0@@ -299,12 +299,15 @@ void cache_flush(struct nft_ctx *nft, enum cmd_ops cmd, struct list_head *msgs)__cache_flush (&cache->list); cache->genid =mnl_genid_get (&ctx); + cache->cmd =CMD_LIST ; }void cache_release (struct nft_cache *cache) {__cache_flush (&cache->list); cache->genid =0 ; + cache->cmd =CMD_INVALID ; + }/* internal ID to uniquely identify a set in the batch */ Signature · 1 line
2.21.0
Otherwise looks good.
Phil Sutter
reviewerRe: [nft PATCH v4 1/7] src: Fix cache_flush() in cache_needs_more() logic
Quoted 4 lines · Eric Garver
Quoted 1 line · Phil Sutter
Commit
34a20645d54faenabled cache updates depending on command causingActual hash is
eeda228c2d17.
Oh, thanks for the catch! No idea where the other hash came from, but it exists in my repository at least.
Thanks, Phil
Re: [nft PATCH v4 7/7] src: Support intra-transaction rule references
In reply to Phil SutterHi Phil,
This series is close. I see one more issue below.
Quoted 29 lines · Phil Sutter
A rule may be added before or after another one using index keyword. To support for the other rule being added within the same batch, one has to make use of
NFTNL_RULE_IDandNFTNL_RULE_POSITION_IDattributes. This patch does just that among a few more crucial things:* Fetch full kernel ruleset upon encountering a rule which references another one. Any earlier rule add/insert commands are then restored by
cache_add_commands().* Avoid cache updates for rules not referencing another one, but make sure cache is not treated as complete afterwards so a later rule with reference will cause cache update and repopulation from command list.
* Reduce
rule_translate_index()to its core code which is the actual linking of rules and consequently rename the function. The removed bits are pulled into the callingrule_evaluate()to reduce code duplication in between cache inserts with and without rule reference.* Pass the current command op to
rule_evaluate()as indicator whether to insert before or after a referenced rule or at beginning or end of chain in cache. Exploit this fromchain_evaluate()to avoid adding the chain's rules a second time.Light casts shadow though: It has been possible to reference another rule of the same transaction via its *guessed* handle - this patch removes that possibility.
Signed-off-by Phil Sutter <[email protected]>---
[..]
Quoted 64 lines
src/rule.c +42
-0@@ -293,6 +293,23 @@ static int cache_add_set_cmd(struct eval_ctx *ectx)return 0 ; } +static int cache_add_rule_cmd (struct eval_ctx *ectx) +{ +struct table *table; +struct chain *chain; + + table =table_lookup (&ectx->cmd->rule->handle, &ectx->nft->cache); +if (!table) +return table_not_found (ectx); + + chain =chain_lookup (table, &ectx->cmd->rule->handle); +if (!chain) +return chain_not_found (ectx); + +rule_cache_update (ectx->cmd->op, chain, ectx->cmd->rule,NULL ); +return 0 ; +} +static int cache_add_commands (struct nft_ctx *nft,struct list_head *msgs) {struct eval_ctx ectx = {@@ -314,6 +331,11 @@ static int cache_add_commands(struct nft_ctx *nft, struct list_head *msgs)continue ; ret =cache_add_set_cmd (&ectx);break ; +case CMD_OBJ_RULE : +if (!cache_is_complete (&nft->cache,CMD_LIST )) +continue ; + ret =cache_add_rule_cmd (&ectx); +break ;default :break ; }@@ -727,6 +749,37 @@ struct rule *rule_lookup_by_index(const struct chain *chain, uint64_t index)return NULL ; } +void rule_cache_update (enum cmd_ops op,struct chain *chain, +struct rule *rule,struct rule *ref) +{ +switch (op) { +case CMD_INSERT : +rule_get (rule); +if (ref) +list_add_tail (&rule->list, &ref->list); +else +list_add (&rule->list, &chain->rules); +break ; +case CMD_ADD : +rule_get (rule); +if (ref) +list_add (&rule->list, &ref->list); +else +list_add_tail (&rule->list, &chain->rules); +break ; +case CMD_REPLACE : +rule_get (rule);
src/rule.c struct rule *rule_lookup_by_index(const struct chain *chain, uint64_t index) · line 781
Quoted 10 lines
src/rule.c +10
-0+list_add (&rule->list, &ref->list); +/* fall through */ +case CMD_DELETE : +list_del (&ref->list); +rule_free (ref); +break ; +default : +break ; + } +}
I'm seeing a NULL pointer dereferenced here. It occurs when we delete a rule and add a new rule using the "index" keyword in the same transaction/batch.
FWIW, I've also got Pablo's recv buffer size patches applied.
# nft --handle list table inet foobar
table inet foobar { # handle 4004
chain foo_chain { # handle 1
accept # handle 2
log # handle 3
}
}# nft -f - delete rule inet foobar foo_chain handle 3 add rule inet foobar foo_chain index 0 drop Segmentation fault (core dumped)
# nft --handle list table inet foobar
table inet foobar { # handle 4004
chain foo_chain { # handle 1
accept # handle 2
log # handle 3
}
}(gdb) bt
#0 0x00007f76d3d6b9b2 in rule_cache_update (op=CMD_DELETE, chain=0x1865d70, rule=0x0, ref=0x0) at rule.c:755 #1 0x00007f76d3d6d16b in cache_add_rule_cmd (ectx=0x7fff51d96c00) at rule.c:309 #2 cache_add_commands (msgs=0x7fff51dab4e0, nft=0x17fba20) at rule.c:337 #3 cache_update (nft=0x17fba20, cmd=cmd@entry=CMD_LIST, msgs=0x7fff51dab4e0) at rule.c:381 #4 0x00007f76d3d7a261 in rule_evaluate (ctx=0x17fc160, rule=0x180df30, op=CMD_ADD) at evaluate.c:3249 #5 0x00007f76d3da423a in nft_parse (nft=nft@entry=0x17fba20, scanner=0x1824060, state=0x17fbb80) at parser_bison.y:799 #6 0x00007f76d3d91324 in nft_parse_bison_filename (cmds=0x17fbae0, msgs=0x7fff51dab4e0, filename=0x7f76d3db8385 "/dev/stdin", nft=0x17fba20) at libnftables.c:380 #7 nft_run_cmd_from_filename (nft=0x17fba20, filename=0x7f76d3db8385 "/dev/stdin", filename@entry=0x7fff51dac67d "-") at libnftables.c:446 #8 0x0000000000401698 in main (argc=3, argv=0x7fff51dab658) at main.c:310
(gdb) frame 1
#1 0x00007f76d3d6d16b in cache_add_rule_cmd (ectx=0x7fff51d96c00) at rule.c:309
309 rule_cache_update(ectx->cmd->op, chain, ectx->cmd->rule, NULL);
(gdb) print *ectx
$1 = {nft = 0x17fba20, msgs = 0x7fff51dab4e0, cmd = 0x1801730, table = 0x0, rule = 0x0, set = 0x0, stmt = 0x0, ectx = {dtype = 0x0, byteorder = BYTEORDER_INVALID, len = 0, maxval = 0}, pctx = {debug_mask = 0, family = 0, protocol = {{location = {indesc = 0x0, {{
token_offset = 0, line_offset = 0, first_line = 0, last_line = 0, first_column = 0, last_column = 0}, {nle = 0x0}}}, desc = 0x0, offset = 0}, {location = {indesc = 0x0, {{token_offset = 0, line_offset = 0, first_line = 0, last_line = 0, first_column = 0,
last_column = 0}, {nle = 0x0}}}, desc = 0x0, offset = 0}, {location = {indesc = 0x0, {{token_offset = 0, line_offset = 0, first_line = 0, last_line = 0, first_column = 0, last_column = 0}, {nle = 0x0}}}, desc = 0x0, offset = 0}, {location = {indesc = 0x0, {{
token_offset = 0, line_offset = 0, first_line = 0, last_line = 0, first_column = 0, last_column = 0}, {nle = 0x0}}}, desc = 0x0, offset = 0}}}}(gdb) print *ectx->cmd
$2 = {list = {next = 0x17fbae0, prev = 0x17fbae0}, location = {indesc = 0x17fbb88, {{token_offset = 6, line_offset = 0, first_line = 1, last_line = 1, first_column = 1, last_column = 43}, {nle = 0x6}}}, op = CMD_DELETE, obj = CMD_OBJ_RULE, handle = {family = 1, table = {
location = {indesc = 0x17fbb88, {{token_offset = 23, line_offset = 0, first_line = 1, last_line = 1, first_column = 18, last_column = 23}, {nle = 0x17}}}, name = 0x18014c0 "foobar"}, chain = {location = {indesc = 0x17fbb88, {{token_offset = 33, line_offset = 0,
first_line = 1, last_line = 1, first_column = 25, last_column = 33}, {nle = 0x21}}}, name = 0x180a740 "foo_chain"}, set = {location = {indesc = 0x0, {{token_offset = 0, line_offset = 0, first_line = 0, last_line = 0, first_column = 0, last_column = 0}, {
nle = 0x0}}}, name = 0x0}, obj = {location = {indesc = 0x0, {{token_offset = 0, line_offset = 0, first_line = 0, last_line = 0, first_column = 0, last_column = 0}, {nle = 0x0}}}, name = 0x0}, flowtable = 0x0, handle = {location = {indesc = 0x17fbb88, {{
token_offset = 40, line_offset = 0, first_line = 1, last_line = 1, first_column = 35, last_column = 42}, {nle = 0x28}}}, id = 3}, position = {location = {indesc = 0x0, {{token_offset = 0, line_offset = 0, first_line = 0, last_line = 0, first_column = 0,
last_column = 0}, {nle = 0x0}}}, id = 0}, index = {location = {indesc = 0x0, {{token_offset = 0, line_offset = 0, first_line = 0, last_line = 0, first_column = 0, last_column = 0}, {nle = 0x0}}}, id = 0}, set_id = 0, rule_id = 0, position_id = 0}, seqnum = 0, {data = 0x0, expr = 0x0, set = 0x0, rule = 0x0, chain = 0x0, table = 0x0, flowtable = 0x0, monitor = 0x0, markup = 0x0, object = 0x0}, arg = 0x0}
(gdb) print ectx->cmd->rule
$3 = (struct rule *) 0x0
Pablo Neira Ayuso
maintainerRe: [nft PATCH v4 7/7] src: Support intra-transaction rule references
Quoted 70 lines · Eric Garver
[..]
Quoted 69 lines
src/rule.c +47
-0@@ -293,6 +293,23 @@ static int cache_add_set_cmd(struct eval_ctx *ectx)return 0 ; } +static int cache_add_rule_cmd (struct eval_ctx *ectx) +{ +struct table *table; +struct chain *chain; + + table =table_lookup (&ectx->cmd->rule->handle, &ectx->nft->cache); +if (!table) +return table_not_found (ectx); + + chain =chain_lookup (table, &ectx->cmd->rule->handle); +if (!chain) +return chain_not_found (ectx); + +rule_cache_update (ectx->cmd->op, chain, ectx->cmd->rule,NULL ); +return 0 ; +} +static int cache_add_commands (struct nft_ctx *nft,struct list_head *msgs) {struct eval_ctx ectx = {@@ -314,6 +331,11 @@ static int cache_add_commands(struct nft_ctx *nft, struct list_head *msgs)continue ; ret =cache_add_set_cmd (&ectx);break ; +case CMD_OBJ_RULE : +if (!cache_is_complete (&nft->cache,CMD_LIST )) +continue ; + ret =cache_add_rule_cmd (&ectx); +break ;default :break ; }@@ -727,6 +749,37 @@ struct rule *rule_lookup_by_index(const struct chain *chain, uint64_t index)return NULL ; } +void rule_cache_update (enum cmd_ops op,struct chain *chain, +struct rule *rule,struct rule *ref) +{ +switch (op) { +case CMD_INSERT : +rule_get (rule); +if (ref) +list_add_tail (&rule->list, &ref->list); +else +list_add (&rule->list, &chain->rules); +break ; +case CMD_ADD : +rule_get (rule); +if (ref) +list_add (&rule->list, &ref->list); +else +list_add_tail (&rule->list, &chain->rules); +break ; +case CMD_REPLACE : +rule_get (rule); +list_add (&rule->list, &ref->list); +/* fall through */ +case CMD_DELETE : +list_del (&ref->list); +rule_free (ref);
Quoted 8 lines
src/rule.c struct rule *rule_lookup_by_index(const struct chain *chain, uint64_t index) · line 781
Quoted 5 lines
src/rule.c +5
-0+break ; +default : +break ; + } +}I'm seeing a NULL pointer dereferenced here. It occurs when we delete a rule and add a new rule using the "index" keyword in the same transaction/batch.
I think we need two new things here:
#1 We need a new initial step, before evalution, to calculate the cache
completeness level. This means, we interate over the batch to see what kind of completeness is needed. Then, cache is fetched only once, at the beginning of the batch processing. Ensure that cache is consistent from that step.
#2 Update the cache incrementally: Add new objects from the evaluation
phase. If RESTART is hit, then release the cache, and restart the evaluation. Probably we don't need to restart the evaluation, just a function to refresh the batch, ie. check if several objects are there.
Phil Sutter
reviewerRe: [nft PATCH v4 7/7] src: Support intra-transaction rule references
Hi,
Quoted 1 line · Pablo Neira Ayuso
On Fri, May 31, 2019 at 12:56:25PM -0400, Eric Garver wrote:
[...]
Quoted 2 lines
Quoted 2 lines
I'm seeing a NULL pointer dereferenced here. It occurs when we delete a rule and add a new rule using the "index" keyword in the same transaction/batch.
Yes, cache population for rule delete command was completely broken. I missed that cmd->rule is NULL in that case, sorry for the mess.
Quoted 8 lines
I think we need two new things here:
#1 We need a new initial step, before evalution, to calculate the cache
completeness level. This means, we interate over the batch to see what kind of completeness is needed. Then, cache is fetched only once, at the beginning of the batch processing. Ensure that cache is consistent from that step.
Quoted 5 lines
#2 Update the cache incrementally: Add new objects from the evaluation
phase. If RESTART is hit, then release the cache, and restart the evaluation. Probably we don't need to restart the evaluation, just a function to refresh the batch, ie. check if several objects are there.
I don't understand this but please wait a day or two before jumping in. I'm currently working on fixing the problem above and some more I found along the way.
Cheers, Phil