]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: fix 'describe' command when passing wrong expressions
authorPablo Neira Ayuso <pablo@netfilter.org>
Tue, 16 Sep 2014 09:03:57 +0000 (11:03 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 17 Sep 2014 08:17:38 +0000 (10:17 +0200)
Before this patch:

 # nft describe tcp foo
 value expression, datatype inet_proto (Internet protocol) (basetype integer), 8 bits
 Segmentation fault

After this patch:

 # nft describe tcp foo
 <cmdline>:1:14-16: Error: syntax error, unexpected string, expecting end of file or newline or semicolon
 describe tcp foo
              ^^^

Reported-by: Kevin Fenzi <kevin@scrye.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/rule.h
src/evaluate.c
src/parser.y
src/rule.c

index db9140687bde7f7ca10091f783ccb5f3c7d888cb..88aefc69eef564d26c669b40041c6868f770475b 100644 (file)
@@ -218,6 +218,7 @@ extern void set_print_plain(const struct set *s);
  * @CMD_RENAME:                rename object
  * @CMD_EXPORT:                export the ruleset in a given format
  * @CMD_MONITOR:       event listener
+ * @CMD_DESCRIBE:      describe an expression
  */
 enum cmd_ops {
        CMD_INVALID,
@@ -230,6 +231,7 @@ enum cmd_ops {
        CMD_RENAME,
        CMD_EXPORT,
        CMD_MONITOR,
+       CMD_DESCRIBE,
 };
 
 /**
@@ -243,6 +245,7 @@ enum cmd_ops {
  * @CMD_OBJ_CHAIN:     chain
  * @CMD_OBJ_TABLE:     table
  * @CMD_OBJ_RULESET:   ruleset
+ * @CMD_OBJ_EXPR:      expression
  */
 enum cmd_obj {
        CMD_OBJ_INVALID,
@@ -253,6 +256,7 @@ enum cmd_obj {
        CMD_OBJ_CHAIN,
        CMD_OBJ_TABLE,
        CMD_OBJ_RULESET,
+       CMD_OBJ_EXPR,
 };
 
 /**
index f66a8ea329910e3da8f560da266e182c04fd6168..34558fcb4f6bd460232b53cff30d4365831787bc 100644 (file)
@@ -1443,6 +1443,7 @@ int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd)
        case CMD_RENAME:
        case CMD_EXPORT:
        case CMD_MONITOR:
+       case CMD_DESCRIBE:
                return 0;
        default:
                BUG("invalid command operation %u\n", cmd->op);
index 653c764900f0818e2bb9a6dc17d76f907a4beb94..ac3d890f6c0b7da8017dd59cd682cc45bdc589c0 100644 (file)
@@ -383,8 +383,8 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %type <cmd>                    line
 %destructor { cmd_free($$); }  line
 
-%type <cmd>                    base_cmd add_cmd create_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd monitor_cmd
-%destructor { cmd_free($$); }  base_cmd add_cmd create_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd monitor_cmd
+%type <cmd>                    base_cmd add_cmd create_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd monitor_cmd describe_cmd
+%destructor { cmd_free($$); }  base_cmd add_cmd create_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd monitor_cmd describe_cmd
 
 %type <handle>                 table_spec tables_spec chain_spec chain_identifier ruleid_spec ruleset_spec
 %destructor { handle_free(&$$); } table_spec tables_spec chain_spec chain_identifier ruleid_spec ruleset_spec
@@ -614,12 +614,7 @@ base_cmd           :       /* empty */     add_cmd         { $$ = $1; }
                        |       RENAME          rename_cmd      { $$ = $2; }
                        |       EXPORT          export_cmd      { $$ = $2; }
                        |       MONITOR         monitor_cmd     { $$ = $2; }
-                       |       DESCRIBE        primary_expr
-                       {
-                               expr_describe($2);
-                               expr_free($2);
-                               $$ = NULL;
-                       }
+                       |       DESCRIBE        describe_cmd    { $$ = $2; }
                        ;
 
 add_cmd                        :       TABLE           table_spec
@@ -865,6 +860,14 @@ monitor_object             :       /* empty */
                        }
                        ;
 
+describe_cmd           :       primary_expr
+                       {
+                               struct handle h = { .family = NFPROTO_UNSPEC };
+                               $$ = cmd_alloc(CMD_DESCRIBE, CMD_OBJ_EXPR, &h, &@$, NULL);
+                               $$->expr = $1;
+                       }
+                       ;
+
 output_format          :       /* empty */
                        {
                                $$ = NFT_OUTPUT_DEFAULT;
index cb2a2285454659900121c7d996e08cba2090a36d..80deb1b9524ca6f23b38aca7a55905307c4cdaaa 100644 (file)
@@ -548,6 +548,9 @@ void cmd_free(struct cmd *cmd)
                case CMD_OBJ_TABLE:
                        table_free(cmd->table);
                        break;
+               case CMD_OBJ_EXPR:
+                       expr_free(cmd->expr);
+                       break;
                default:
                        BUG("invalid command object type %u\n", cmd->obj);
                }
@@ -909,6 +912,12 @@ static int do_command_monitor(struct netlink_ctx *ctx, struct cmd *cmd)
        return netlink_monitor(&monhandler);
 }
 
+static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd)
+{
+       expr_describe(cmd->expr);
+       return 0;
+}
+
 int do_command(struct netlink_ctx *ctx, struct cmd *cmd)
 {
        switch (cmd->op) {
@@ -930,6 +939,8 @@ int do_command(struct netlink_ctx *ctx, struct cmd *cmd)
                return do_command_export(ctx, cmd);
        case CMD_MONITOR:
                return do_command_monitor(ctx, cmd);
+       case CMD_DESCRIBE:
+               return do_command_describe(ctx, cmd);
        default:
                BUG("invalid command object type %u\n", cmd->obj);
        }