netfilter: nf_tables: upfront validation of data via nft_data_init()
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v2 1/2 netfilter: nf_tables: upfront validation of data via nft_data_init()
Instead of parsing the data and then validate that type and length are correct, pass a description of the expected data so it can be validated upfront before parsing it to bail out earlier.
This patch adds a new .size field to specify the maximum size of the data area. The .len field is optional and it is used as an input/output field, it provides the specific length of the expected data in the input path. If then .len field is not specified, then obtained length from the netlink attribute is stored. This is required by cmp, bitwise, range and immediate, which provide no netlink attribute that describes the data length. The immediate expression uses the destination register type to infer the expected data type.
Relying on opencoded validation of the expected data might lead to subtle bugs as described in 7e6bc1f6cabc ("netfilter: nf_tables:
stricter validation of element data").
---
v2: remove desc->type = NFT_DATA_VERDICT in nft_verdict_init(), this is
already set by caller.
include/net/netfilter/nf_tables.h | 4 +- net/netfilter/nf_tables_api.c | 78 ++++++++++++++++--------------- net/netfilter/nft_bitwise.c | 66 +++++++++++++------------- net/netfilter/nft_cmp.c | 44 ++++++++--------- net/netfilter/nft_immediate.c | 22 +++++++-- net/netfilter/nft_range.c | 27 +++++------ 6 files changed, 126 insertions(+), 115 deletions(-)
include/net/netfilter/nf_tables.h +2 -2
@@ -223,11 +223,11 @@ struct nft_ctx {struct nft_data_desc {enum nft_data_types type; +unsigned int size;unsigned int len; }; -int nft_data_init (const struct nft_ctx *ctx, -struct nft_data *data,unsigned int size, +int nft_data_init (const struct nft_ctx *ctx,struct nft_data *data,struct nft_data_desc *desc,const struct nlattr *nla);void nft_data_hold (const struct nft_data *data,enum nft_data_types type);void nft_data_release (const struct nft_data *data,enum nft_data_types type);
net/netfilter/nf_tables_api.c +40 -38
141 lines not on this page · raw message
net/netfilter/nft_bitwise.c +33 -33
112 lines not on this page · raw message
net/netfilter/nft_cmp.c +20 -24
94 lines not on this page · raw message
net/netfilter/nft_immediate.c +19 -3
@@ -29,20 +29,36 @@ static const struct nla_policy nft_immediate_policy[NFTA_IMMEDIATE_MAX + 1] = { [NFTA_IMMEDIATE_DATA] = { .type = NLA_NESTED }, }; +static enum nft_data_types nft_reg_to_type (const struct nlattr *nla) +{ +enum nft_data_types type; +u8 reg; + + reg =ntohl (nla_get_be32 (nla)); +if (reg ==NFT_REG_VERDICT ) + type =NFT_DATA_VERDICT ; +else + type =NFT_DATA_VALUE ; + +return type; +} +static int nft_immediate_init (const struct nft_ctx *ctx,const struct nft_expr *expr,const struct nlattr *const tb[]) {struct nft_immediate_expr *priv =nft_expr_priv (expr); -struct nft_data_desc desc; +struct nft_data_desc desc = { + .size =sizeof (priv->data), + };int err;if (tb[NFTA_IMMEDIATE_DREG ] ==NULL || tb[NFTA_IMMEDIATE_DATA ] ==NULL )return -EINVAL ; - err =nft_data_init (ctx, &priv->data,sizeof (priv->data), &desc, - tb[NFTA_IMMEDIATE_DATA ]); + desc.type =nft_reg_to_type (tb[NFTA_IMMEDIATE_DREG ]); + err =nft_data_init (ctx, &priv->data, &desc, tb[NFTA_IMMEDIATE_DATA ]);if (err <0 )return err;
net/netfilter/nft_range.c +12 -15
@@ -51,7 +51,14 @@ static int nft_range_init(const struct nft_ctx *ctx, const struct nft_expr *expr const struct nlattr * const tb[]) {struct nft_range_expr *priv =nft_expr_priv (expr); -struct nft_data_desc desc_from, desc_to; +struct nft_data_desc desc_from = { + .type =NFT_DATA_VALUE , + .size =sizeof (priv->data_from), + }; +struct nft_data_desc desc_to = { + .type =NFT_DATA_VALUE , + .size =sizeof (priv->data_to), + };int err;u32 op;
@@ -61,26 +68,16 @@ static int nft_range_init(const struct nft_ctx *ctx, const struct nft_expr *expr !tb[NFTA_RANGE_TO_DATA])return -EINVAL ; - err =nft_data_init (NULL , &priv->data_from,sizeof (priv->data_from), - &desc_from, tb[NFTA_RANGE_FROM_DATA ]); + err =nft_data_init (NULL , &priv->data_from, &desc_from, + tb[NFTA_RANGE_FROM_DATA ]);if (err <0 )return err; -if (desc_from.type !=NFT_DATA_VALUE ) { - err = -EINVAL ; - goto err1; - } - - err =nft_data_init (NULL , &priv->data_to,sizeof (priv->data_to), - &desc_to, tb[NFTA_RANGE_TO_DATA ]); + err =nft_data_init (NULL , &priv->data_to, &desc_to, + tb[NFTA_RANGE_TO_DATA ]);if (err <0 ) goto err1; -if (desc_to.type !=NFT_DATA_VALUE ) { - err = -EINVAL ; - goto err2; - } -if (desc_from.len != desc_to.len) { err = -EINVAL ; goto err2;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/2 netfilter: nf_tables: disallow jump to implicit chain from set element
Extend struct nft_data_desc to add a flag field that specifies nft_data_init() is being called for set element data.
Use it to disallow jump to implicit chain from set element, only jump to chain via immediate expression is allowed.
---
requires 1/2, new in this series.
include/net/netfilter/nf_tables.h | 5 +++++ net/netfilter/nf_tables_api.c | 4 ++++ 2 files changed, 9 insertions(+)
include/net/netfilter/nf_tables.h +5 -0
@@ -221,10 +221,15 @@ struct nft_ctx {bool report; }; +enum nft_data_desc_flags { +NFT_DATA_DESC_SETELEM = (1 <<0 ), +}; +struct nft_data_desc {enum nft_data_types type;unsigned int size;unsigned int len; +unsigned int flags; }; int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
net/netfilter/nf_tables_api.c +4 -0
@@ -5219,6 +5219,7 @@ static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set, desc->type = dtype; desc->size =NFT_DATA_VALUE_MAXLEN ; desc->len = set->dlen; + desc->flags =NFT_DATA_DESC_SETELEM ;return nft_data_init (ctx, data, desc, attr); }
@@ -9658,6 +9659,9 @@ static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,return PTR_ERR (chain);if (nft_is_base_chain (chain))return -EOPNOTSUPP ; +if (desc->flags &NFT_DATA_DESC_SETELEM && + chain->flags &NFT_CHAIN_BINDING ) +return -EINVAL ; chain->use++; data->verdict.chain = chain;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerQuoted 13 lines · Pablo Neira Ayuso
Instead of parsing the data and then validate that type and length are correct, pass a description of the expected data so it can be validated upfront before parsing it to bail out earlier.
This patch adds a new .size field to specify the maximum size of the data area. The .len field is optional and it is used as an input/output field, it provides the specific length of the expected data in the input path. If then .len field is not specified, then obtained length from the netlink attribute is stored. This is required by cmp, bitwise, range and immediate, which provide no netlink attribute that describes the data length. The immediate expression uses the destination register type to infer the expected data type.
Quoted 3 lines
Relying on opencoded validation of the expected data might lead to subtle bugs as described in
7e6bc1f6cabc("netfilter:nf_tables:
stricter validation of element data").
For the record, this series are applied