netfilter: nf_tables: add nft_setelem_parse_key()

Add helper function to parse the set element key netlink attribute.

v4: No changes
v3: New patch

[sbrivio: refactor error paths and labels; use NFT_DATA_VALUE_MAXLEN
  instead of sizeof(*key) in helper, value can be longer than that;
  rebase]
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
This commit is contained in:
Pablo Neira Ayuso 2020-01-22 00:17:51 +01:00
parent 32efcc06d2
commit 20a1452c35
1 changed files with 45 additions and 46 deletions

View File

@ -4524,11 +4524,28 @@ static int nft_setelem_parse_flags(const struct nft_set *set,
return 0; return 0;
} }
static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set,
struct nft_data *key, struct nlattr *attr)
{
struct nft_data_desc desc;
int err;
err = nft_data_init(ctx, key, NFT_DATA_VALUE_MAXLEN, &desc, attr);
if (err < 0)
return err;
if (desc.type != NFT_DATA_VALUE || desc.len != set->klen) {
nft_data_release(key, desc.type);
return -EINVAL;
}
return 0;
}
static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set,
const struct nlattr *attr) const struct nlattr *attr)
{ {
struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
struct nft_data_desc desc;
struct nft_set_elem elem; struct nft_set_elem elem;
struct sk_buff *skb; struct sk_buff *skb;
uint32_t flags = 0; uint32_t flags = 0;
@ -4547,17 +4564,11 @@ static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set,
if (err < 0) if (err < 0)
return err; return err;
err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc, err = nft_setelem_parse_key(ctx, set, &elem.key.val,
nla[NFTA_SET_ELEM_KEY]); nla[NFTA_SET_ELEM_KEY]);
if (err < 0) if (err < 0)
return err; return err;
err = -EINVAL;
if (desc.type != NFT_DATA_VALUE || desc.len != set->klen) {
nft_data_release(&elem.key.val, desc.type);
return err;
}
priv = set->ops->get(ctx->net, set, &elem, flags); priv = set->ops->get(ctx->net, set, &elem, flags);
if (IS_ERR(priv)) if (IS_ERR(priv))
return PTR_ERR(priv); return PTR_ERR(priv);
@ -4756,13 +4767,13 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
{ {
struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
u8 genmask = nft_genmask_next(ctx->net); u8 genmask = nft_genmask_next(ctx->net);
struct nft_data_desc d1, d2;
struct nft_set_ext_tmpl tmpl; struct nft_set_ext_tmpl tmpl;
struct nft_set_ext *ext, *ext2; struct nft_set_ext *ext, *ext2;
struct nft_set_elem elem; struct nft_set_elem elem;
struct nft_set_binding *binding; struct nft_set_binding *binding;
struct nft_object *obj = NULL; struct nft_object *obj = NULL;
struct nft_userdata *udata; struct nft_userdata *udata;
struct nft_data_desc desc;
struct nft_data data; struct nft_data data;
enum nft_registers dreg; enum nft_registers dreg;
struct nft_trans *trans; struct nft_trans *trans;
@ -4828,15 +4839,12 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
return err; return err;
} }
err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &d1, err = nft_setelem_parse_key(ctx, set, &elem.key.val,
nla[NFTA_SET_ELEM_KEY]); nla[NFTA_SET_ELEM_KEY]);
if (err < 0) if (err < 0)
goto err1; goto err1;
err = -EINVAL;
if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
goto err2;
nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, d1.len); nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
if (timeout > 0) { if (timeout > 0) {
nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION); nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
if (timeout != set->timeout) if (timeout != set->timeout)
@ -4859,13 +4867,13 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
} }
if (nla[NFTA_SET_ELEM_DATA] != NULL) { if (nla[NFTA_SET_ELEM_DATA] != NULL) {
err = nft_data_init(ctx, &data, sizeof(data), &d2, err = nft_data_init(ctx, &data, sizeof(data), &desc,
nla[NFTA_SET_ELEM_DATA]); nla[NFTA_SET_ELEM_DATA]);
if (err < 0) if (err < 0)
goto err2; goto err2;
err = -EINVAL; err = -EINVAL;
if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen) if (set->dtype != NFT_DATA_VERDICT && desc.len != set->dlen)
goto err3; goto err3;
dreg = nft_type_to_reg(set->dtype); dreg = nft_type_to_reg(set->dtype);
@ -4882,18 +4890,18 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
err = nft_validate_register_store(&bind_ctx, dreg, err = nft_validate_register_store(&bind_ctx, dreg,
&data, &data,
d2.type, d2.len); desc.type, desc.len);
if (err < 0) if (err < 0)
goto err3; goto err3;
if (d2.type == NFT_DATA_VERDICT && if (desc.type == NFT_DATA_VERDICT &&
(data.verdict.code == NFT_GOTO || (data.verdict.code == NFT_GOTO ||
data.verdict.code == NFT_JUMP)) data.verdict.code == NFT_JUMP))
nft_validate_state_update(ctx->net, nft_validate_state_update(ctx->net,
NFT_VALIDATE_NEED); NFT_VALIDATE_NEED);
} }
nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, d2.len); nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len);
} }
/* The full maximum length of userdata can exceed the maximum /* The full maximum length of userdata can exceed the maximum
@ -4976,9 +4984,9 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
kfree(elem.priv); kfree(elem.priv);
err3: err3:
if (nla[NFTA_SET_ELEM_DATA] != NULL) if (nla[NFTA_SET_ELEM_DATA] != NULL)
nft_data_release(&data, d2.type); nft_data_release(&data, desc.type);
err2: err2:
nft_data_release(&elem.key.val, d1.type); nft_data_release(&elem.key.val, NFT_DATA_VALUE);
err1: err1:
return err; return err;
} }
@ -5074,7 +5082,6 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
{ {
struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
struct nft_set_ext_tmpl tmpl; struct nft_set_ext_tmpl tmpl;
struct nft_data_desc desc;
struct nft_set_elem elem; struct nft_set_elem elem;
struct nft_set_ext *ext; struct nft_set_ext *ext;
struct nft_trans *trans; struct nft_trans *trans;
@ -5085,11 +5092,10 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
nft_set_elem_policy, NULL); nft_set_elem_policy, NULL);
if (err < 0) if (err < 0)
goto err1; return err;
err = -EINVAL;
if (nla[NFTA_SET_ELEM_KEY] == NULL) if (nla[NFTA_SET_ELEM_KEY] == NULL)
goto err1; return -EINVAL;
nft_set_ext_prepare(&tmpl); nft_set_ext_prepare(&tmpl);
@ -5099,37 +5105,31 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
if (flags != 0) if (flags != 0)
nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc, err = nft_setelem_parse_key(ctx, set, &elem.key.val,
nla[NFTA_SET_ELEM_KEY]); nla[NFTA_SET_ELEM_KEY]);
if (err < 0) if (err < 0)
goto err1; return err;
err = -EINVAL; nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
goto err2;
nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, desc.len);
err = -ENOMEM; err = -ENOMEM;
elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, NULL, 0, elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, NULL, 0,
0, GFP_KERNEL); 0, GFP_KERNEL);
if (elem.priv == NULL) if (elem.priv == NULL)
goto err2; goto fail_elem;
ext = nft_set_elem_ext(set, elem.priv); ext = nft_set_elem_ext(set, elem.priv);
if (flags) if (flags)
*nft_set_ext_flags(ext) = flags; *nft_set_ext_flags(ext) = flags;
trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
if (trans == NULL) { if (trans == NULL)
err = -ENOMEM; goto fail_trans;
goto err3;
}
priv = set->ops->deactivate(ctx->net, set, &elem); priv = set->ops->deactivate(ctx->net, set, &elem);
if (priv == NULL) { if (priv == NULL) {
err = -ENOENT; err = -ENOENT;
goto err4; goto fail_ops;
} }
kfree(elem.priv); kfree(elem.priv);
elem.priv = priv; elem.priv = priv;
@ -5140,13 +5140,12 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
list_add_tail(&trans->list, &ctx->net->nft.commit_list); list_add_tail(&trans->list, &ctx->net->nft.commit_list);
return 0; return 0;
err4: fail_ops:
kfree(trans); kfree(trans);
err3: fail_trans:
kfree(elem.priv); kfree(elem.priv);
err2: fail_elem:
nft_data_release(&elem.key.val, desc.type); nft_data_release(&elem.key.val, NFT_DATA_VALUE);
err1:
return err; return err;
} }