Skip to content

Commit fe92ca4

Browse files
kaberPablo Neira Ayuso
authored andcommitted
netfilter: nft_ct: split nft_ct_init() into two functions for get/set
For value spanning multiple registers, we need to validate the length of data loads. In order to add this to nft_ct, we need the length from key validation. Split the nft_ct_init() function into two functions for the get and set operations as preparation for that. Signed-off-by: Patrick McHardy <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent d2caa69 commit fe92ca4

File tree

1 file changed

+43
-53
lines changed

1 file changed

+43
-53
lines changed

net/netfilter/nft_ct.c

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -215,22 +215,14 @@ static void nft_ct_l3proto_module_put(uint8_t family)
215215
nf_ct_l3proto_module_put(family);
216216
}
217217

218-
static int nft_ct_init_validate_get(const struct nft_expr *expr,
219-
const struct nlattr * const tb[])
218+
static int nft_ct_get_init(const struct nft_ctx *ctx,
219+
const struct nft_expr *expr,
220+
const struct nlattr * const tb[])
220221
{
221222
struct nft_ct *priv = nft_expr_priv(expr);
223+
int err;
222224

223-
if (tb[NFTA_CT_DIRECTION] != NULL) {
224-
priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
225-
switch (priv->dir) {
226-
case IP_CT_DIR_ORIGINAL:
227-
case IP_CT_DIR_REPLY:
228-
break;
229-
default:
230-
return -EINVAL;
231-
}
232-
}
233-
225+
priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
234226
switch (priv->key) {
235227
case NFT_CT_STATE:
236228
case NFT_CT_DIRECTION:
@@ -262,12 +254,42 @@ static int nft_ct_init_validate_get(const struct nft_expr *expr,
262254
return -EOPNOTSUPP;
263255
}
264256

257+
if (tb[NFTA_CT_DIRECTION] != NULL) {
258+
priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
259+
switch (priv->dir) {
260+
case IP_CT_DIR_ORIGINAL:
261+
case IP_CT_DIR_REPLY:
262+
break;
263+
default:
264+
return -EINVAL;
265+
}
266+
}
267+
268+
priv->dreg = ntohl(nla_get_be32(tb[NFTA_CT_DREG]));
269+
err = nft_validate_output_register(priv->dreg);
270+
if (err < 0)
271+
return err;
272+
273+
err = nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
274+
if (err < 0)
275+
return err;
276+
277+
err = nft_ct_l3proto_try_module_get(ctx->afi->family);
278+
if (err < 0)
279+
return err;
280+
265281
return 0;
266282
}
267283

268-
static int nft_ct_init_validate_set(uint32_t key)
284+
static int nft_ct_set_init(const struct nft_ctx *ctx,
285+
const struct nft_expr *expr,
286+
const struct nlattr * const tb[])
269287
{
270-
switch (key) {
288+
struct nft_ct *priv = nft_expr_priv(expr);
289+
int err;
290+
291+
priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
292+
switch (priv->key) {
271293
#ifdef CONFIG_NF_CONNTRACK_MARK
272294
case NFT_CT_MARK:
273295
break;
@@ -276,42 +298,10 @@ static int nft_ct_init_validate_set(uint32_t key)
276298
return -EOPNOTSUPP;
277299
}
278300

279-
return 0;
280-
}
281-
282-
static int nft_ct_init(const struct nft_ctx *ctx,
283-
const struct nft_expr *expr,
284-
const struct nlattr * const tb[])
285-
{
286-
struct nft_ct *priv = nft_expr_priv(expr);
287-
int err;
288-
289-
priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
290-
291-
if (tb[NFTA_CT_DREG]) {
292-
err = nft_ct_init_validate_get(expr, tb);
293-
if (err < 0)
294-
return err;
295-
296-
priv->dreg = ntohl(nla_get_be32(tb[NFTA_CT_DREG]));
297-
err = nft_validate_output_register(priv->dreg);
298-
if (err < 0)
299-
return err;
300-
301-
err = nft_validate_data_load(ctx, priv->dreg, NULL,
302-
NFT_DATA_VALUE);
303-
if (err < 0)
304-
return err;
305-
} else {
306-
err = nft_ct_init_validate_set(priv->key);
307-
if (err < 0)
308-
return err;
309-
310-
priv->sreg = ntohl(nla_get_be32(tb[NFTA_CT_SREG]));
311-
err = nft_validate_input_register(priv->sreg);
312-
if (err < 0)
313-
return err;
314-
}
301+
priv->sreg = ntohl(nla_get_be32(tb[NFTA_CT_SREG]));
302+
err = nft_validate_input_register(priv->sreg);
303+
if (err < 0)
304+
return err;
315305

316306
err = nft_ct_l3proto_try_module_get(ctx->afi->family);
317307
if (err < 0)
@@ -372,7 +362,7 @@ static const struct nft_expr_ops nft_ct_get_ops = {
372362
.type = &nft_ct_type,
373363
.size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
374364
.eval = nft_ct_get_eval,
375-
.init = nft_ct_init,
365+
.init = nft_ct_get_init,
376366
.destroy = nft_ct_destroy,
377367
.dump = nft_ct_get_dump,
378368
};
@@ -381,7 +371,7 @@ static const struct nft_expr_ops nft_ct_set_ops = {
381371
.type = &nft_ct_type,
382372
.size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
383373
.eval = nft_ct_set_eval,
384-
.init = nft_ct_init,
374+
.init = nft_ct_set_init,
385375
.destroy = nft_ct_destroy,
386376
.dump = nft_ct_set_dump,
387377
};

0 commit comments

Comments
 (0)