Skip to contentSkip to conversation

rule: make cmd_free(NULL) valid

PATCH In review Florian Westphal 2 participants 2 messages Jan 8, 2025 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 rule: make cmd_free(NULL) valid

bison uses cmd_free($$) as destructor, but base_cmd can set it to NULL, e.g.

  |       ELEMENT         set_spec        set_block_expr
  {
    if (nft_cmd_collapse_elems(CMD_ADD, state->cmds, &$2, $3)) {
       handle_free(&$2);
       expr_free($3);
       $$ = NULL;   // cmd set to NULL
       break;
    }
    $$ = cmd_alloc(CMD_ADD, CMD_OBJ_ELEMENTS, &$2, &@$, $3);

expr_free(NULL) is legal, cmd_free() causes crash. So just allow this to avoid cluttering parser_bison.y with "if ($$)".

Also add the afl-generated bogon input to the test files.

---
 src/rule.c                                    |  3 +++
 .../bogons/nft-f/cmd_is_null_on_free          | 20 +++++++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 tests/shell/testcases/bogons/nft-f/cmd_is_null_on_free
src/rule.c +3 -0
index 151ed531969c..cc43cd18b7c7 100644
@@ -1372,6 +1372,9 @@ void monitor_free(struct monitor *m)
 
 void cmd_free(struct cmd *cmd)
 {
+	if (cmd == NULL)
+		return;
+
 	handle_free(&cmd->handle);
 	if (cmd->data != NULL) {
 		switch (cmd->obj) {
tests/shell/testcases/bogons/nft-f/cmd_is_null_on_free new file +20 -0
new file mode 100644
index 000000000000..6a42aa90cd53
@@ -0,0 +1,20 @@
+nt      rootepep test- {
+* : 1:3 }
+        element root tesip {
+* : 1:3 }
+        elent   rootsel s1 {
+        typ�    elements < { "Linux" }
+        }
+tatlet e t {
+        thataepep test- {
+* : 1:3 }
+        element root tesip {
+* : 1:3 }�      table Cridgents < t {
+list            set y p
+        type i , {
+        sel s1 {
+        typ�    elements < { "Linux" }
+        }
+tatlet e t {
+        thatable Cridgents < t {
+lis
Signature · 1 line
2.45.2
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Florian Westphal

bison uses cmd_free($$) as destructor, but base_cmd can set it to NULL, e.g.

  |       ELEMENT         set_spec        set_block_expr
  {
    if (nft_cmd_collapse_elems(CMD_ADD, state->cmds, &$2, $3)) {
       handle_free(&$2);
       expr_free($3);
       $$ = NULL;   // cmd set to NULL
       break;
    }
    $$ = cmd_alloc(CMD_ADD, CMD_OBJ_ELEMENTS, &$2, &@$, $3);
Quoted 6 lines

expr_free(NULL) is legal, cmd_free() causes crash. So just allow this to avoid cluttering parser_bison.y with "if ($$)".

Also add the afl-generated bogon input to the test files.

Thanks

Quoted 50 lines
---
 src/rule.c                                    |  3 +++
 .../bogons/nft-f/cmd_is_null_on_free          | 20 +++++++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 tests/shell/testcases/bogons/nft-f/cmd_is_null_on_free
src/rule.c +3 -0
index 151ed531969c..cc43cd18b7c7 100644
@@ -1372,6 +1372,9 @@ void monitor_free(struct monitor *m)
 
 void cmd_free(struct cmd *cmd)
 {
+	if (cmd == NULL)
+		return;
+
 	handle_free(&cmd->handle);
 	if (cmd->data != NULL) {
 		switch (cmd->obj) {
tests/shell/testcases/bogons/nft-f/cmd_is_null_on_free new file +20 -0
new file mode 100644
index 000000000000..6a42aa90cd53
@@ -0,0 +1,20 @@
+nt      rootepep test- {
+* : 1:3 }
+        element root tesip {
+* : 1:3 }
+        elent   rootsel s1 {
+        typ�    elements < { "Linux" }
+        }
+tatlet e t {
+        thataepep test- {
+* : 1:3 }
+        element root tesip {
+* : 1:3 }�      table Cridgents < t {
+list            set y p
+        type i , {
+        sel s1 {
+        typ�    elements < { "Linux" }
+        }
+tatlet e t {
+        thatable Cridgents < t {
+lis
Signature · 1 line
2.45.2