netfilter: nf_tables: upfront validation of data via nft_data_init()
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 1/1 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").
--- include/net/netfilter/nf_tables.h | 4 +- net/netfilter/nf_tables_api.c | 76 ++++++++++++++++--------------- 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, 125 insertions(+), 114 deletions(-)
include/net/netfilter/nf_tables.h +2 -2
@@ -208,11 +208,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 +39 -37
133 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