]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
expression: cleanup expr_ops_by_type() and handle u32 input
authorThomas Haller <thaller@redhat.com>
Wed, 20 Sep 2023 14:26:08 +0000 (16:26 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 13 Aug 2025 16:55:50 +0000 (18:55 +0200)
commit 3d0ce3c19d319a5aae806b617905cfa1ee7f87f4 upstream.

Make fewer assumptions about the underlying integer type of the enum.
Instead, be clear about where we have an untrusted uint32_t from netlink
and an enum. Rename expr_ops_by_type() to expr_ops_by_type_u32() to make
this clearer. Later we might make the enum as packed, when this starts
to matter more.

Also, only the code path expr_ops() wants strict validation and assert
against valid enum values. Move the assertion out of
__expr_ops_by_type(). Then expr_ops_by_type_u32() does not need to
duplicate the handling of EXPR_INVALID. We still need to duplicate the
check against EXPR_MAX, to ensure that the uint32_t value can be cast to
an enum value.

[ Remove cast on EXPR_MAX. --pablo ]

Signed-off-by: Thomas Haller <thaller@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/expression.h
src/expression.c
src/netlink.c

index 0de2af5485b8cedc07810a4f904dc9d27672d445..b16efd17085ea8859b2bed17e87f5a292d1e7c73 100644 (file)
@@ -194,7 +194,7 @@ struct expr_ops {
 };
 
 const struct expr_ops *expr_ops(const struct expr *e);
-const struct expr_ops *expr_ops_by_type(enum expr_types etype);
+const struct expr_ops *expr_ops_by_type_u32(uint32_t value);
 
 /**
  * enum expr_flags
index dd17f387842772b6766467014d30b52d302da8ab..0496ca75b72585c5edebd42392bad24129adf120 100644 (file)
@@ -1087,7 +1087,7 @@ static struct expr *concat_expr_parse_udata(const struct nftnl_udata *attr)
                        goto err_free;
 
                etype = nftnl_udata_get_u32(nest_ud[NFTNL_UDATA_SET_KEY_CONCAT_SUB_TYPE]);
-               ops = expr_ops_by_type(etype);
+               ops = expr_ops_by_type_u32(etype);
                if (!ops || !ops->parse_udata)
                        goto err_free;
 
@@ -1601,9 +1601,7 @@ void range_expr_value_high(mpz_t rop, const struct expr *expr)
 static const struct expr_ops *__expr_ops_by_type(enum expr_types etype)
 {
        switch (etype) {
-       case EXPR_INVALID:
-               BUG("Invalid expression ops requested");
-               break;
+       case EXPR_INVALID: break;
        case EXPR_VERDICT: return &verdict_expr_ops;
        case EXPR_SYMBOL: return &symbol_expr_ops;
        case EXPR_VARIABLE: return &variable_expr_ops;
@@ -1635,20 +1633,23 @@ static const struct expr_ops *__expr_ops_by_type(enum expr_types etype)
        case EXPR_FLAGCMP: return &flagcmp_expr_ops;
        }
 
-       BUG("Unknown expression type %d\n", etype);
+       return NULL;
 }
 
 const struct expr_ops *expr_ops(const struct expr *e)
 {
-       return __expr_ops_by_type(e->etype);
+       const struct expr_ops *ops;
+
+       ops = __expr_ops_by_type(e->etype);
+       if (!ops)
+               BUG("Unknown expression type %d\n", e->etype);
+
+       return ops;
 }
 
-const struct expr_ops *expr_ops_by_type(uint32_t value)
+const struct expr_ops *expr_ops_by_type_u32(uint32_t value)
 {
-       /* value might come from unreliable source, such as "udata"
-        * annotation of set keys.  Avoid BUG() assertion.
-        */
-       if (value == EXPR_INVALID || value > EXPR_MAX)
+       if (value > EXPR_MAX)
                return NULL;
 
        return __expr_ops_by_type(value);
index 476301eaea7d67a1a877e1217d88088828a567fd..d832a7d7c1cd95cea929e95c6a317ed049ac96c8 100644 (file)
@@ -912,8 +912,8 @@ static struct expr *set_make_key(const struct nftnl_udata *attr)
 {
        const struct nftnl_udata *ud[NFTNL_UDATA_SET_TYPEOF_MAX + 1] = {};
        const struct expr_ops *ops;
-       enum expr_types etype;
        struct expr *expr;
+       uint32_t etype;
        int err;
 
        if (!attr)
@@ -929,7 +929,7 @@ static struct expr *set_make_key(const struct nftnl_udata *attr)
                return NULL;
 
        etype = nftnl_udata_get_u32(ud[NFTNL_UDATA_SET_TYPEOF_EXPR]);
-       ops = expr_ops_by_type(etype);
+       ops = expr_ops_by_type_u32(etype);
        if (!ops)
                return NULL;