Skip to content

Commit 36b701f

Browse files
Laura Garcia Liebanaummakynes
authored andcommitted
netfilter: nf_tables: validate maximum value of u32 netlink attributes
Fetch value and validate u32 netlink attribute. This validation is usually required when the u32 netlink attributes are being stored in a field whose size is smaller. This patch revisits 4da449a ("netfilter: nft_exthdr: Add size check on u8 nft_exthdr attributes"). Fixes: 9651851 ("netfilter: add nftables") Suggested-by: Pablo Neira Ayuso <[email protected]> Signed-off-by: Laura Garcia Liebana <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 2b03bf7 commit 36b701f

File tree

7 files changed

+60
-8
lines changed

7 files changed

+60
-8
lines changed

include/net/netfilter/nf_tables.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
145145
return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
146146
}
147147

148+
unsigned int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
148149
unsigned int nft_parse_register(const struct nlattr *attr);
149150
int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
150151

net/netfilter/nf_tables_api.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4409,6 +4409,31 @@ static int nf_tables_check_loops(const struct nft_ctx *ctx,
44094409
return 0;
44104410
}
44114411

4412+
/**
4413+
* nft_parse_u32_check - fetch u32 attribute and check for maximum value
4414+
*
4415+
* @attr: netlink attribute to fetch value from
4416+
* @max: maximum value to be stored in dest
4417+
* @dest: pointer to the variable
4418+
*
4419+
* Parse, check and store a given u32 netlink attribute into variable.
4420+
* This function returns -ERANGE if the value goes over maximum value.
4421+
* Otherwise a 0 is returned and the attribute value is stored in the
4422+
* destination variable.
4423+
*/
4424+
unsigned int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest)
4425+
{
4426+
int val;
4427+
4428+
val = ntohl(nla_get_be32(attr));
4429+
if (val > max)
4430+
return -ERANGE;
4431+
4432+
*dest = val;
4433+
return 0;
4434+
}
4435+
EXPORT_SYMBOL_GPL(nft_parse_u32_check);
4436+
44124437
/**
44134438
* nft_parse_register - parse a register value from a netlink attribute
44144439
*

net/netfilter/nft_bitwise.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static int nft_bitwise_init(const struct nft_ctx *ctx,
5252
{
5353
struct nft_bitwise *priv = nft_expr_priv(expr);
5454
struct nft_data_desc d1, d2;
55+
u32 len;
5556
int err;
5657

5758
if (tb[NFTA_BITWISE_SREG] == NULL ||
@@ -61,7 +62,12 @@ static int nft_bitwise_init(const struct nft_ctx *ctx,
6162
tb[NFTA_BITWISE_XOR] == NULL)
6263
return -EINVAL;
6364

64-
priv->len = ntohl(nla_get_be32(tb[NFTA_BITWISE_LEN]));
65+
err = nft_parse_u32_check(tb[NFTA_BITWISE_LEN], U8_MAX, &len);
66+
if (err < 0)
67+
return err;
68+
69+
priv->len = len;
70+
6571
priv->sreg = nft_parse_register(tb[NFTA_BITWISE_SREG]);
6672
err = nft_validate_register_load(priv->sreg, priv->len);
6773
if (err < 0)

net/netfilter/nft_byteorder.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ static int nft_byteorder_init(const struct nft_ctx *ctx,
9999
const struct nlattr * const tb[])
100100
{
101101
struct nft_byteorder *priv = nft_expr_priv(expr);
102+
u32 size, len;
102103
int err;
103104

104105
if (tb[NFTA_BYTEORDER_SREG] == NULL ||
@@ -117,7 +118,12 @@ static int nft_byteorder_init(const struct nft_ctx *ctx,
117118
return -EINVAL;
118119
}
119120

120-
priv->size = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_SIZE]));
121+
err = nft_parse_u32_check(tb[NFTA_BYTEORDER_SIZE], U8_MAX, &size);
122+
if (err < 0)
123+
return err;
124+
125+
priv->size = size;
126+
121127
switch (priv->size) {
122128
case 2:
123129
case 4:
@@ -128,7 +134,12 @@ static int nft_byteorder_init(const struct nft_ctx *ctx,
128134
}
129135

130136
priv->sreg = nft_parse_register(tb[NFTA_BYTEORDER_SREG]);
131-
priv->len = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_LEN]));
137+
err = nft_parse_u32_check(tb[NFTA_BYTEORDER_LEN], U8_MAX, &len);
138+
if (err < 0)
139+
return err;
140+
141+
priv->len = len;
142+
132143
err = nft_validate_register_load(priv->sreg, priv->len);
133144
if (err < 0)
134145
return err;

net/netfilter/nft_cmp.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ static int nft_cmp_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
8484
if (err < 0)
8585
return err;
8686

87+
if (desc.len > U8_MAX)
88+
return -ERANGE;
89+
8790
priv->op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
8891
priv->len = desc.len;
8992
return 0;

net/netfilter/nft_exthdr.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,21 @@ static int nft_exthdr_init(const struct nft_ctx *ctx,
5959
const struct nlattr * const tb[])
6060
{
6161
struct nft_exthdr *priv = nft_expr_priv(expr);
62-
u32 offset, len;
62+
u32 offset, len, err;
6363

6464
if (tb[NFTA_EXTHDR_DREG] == NULL ||
6565
tb[NFTA_EXTHDR_TYPE] == NULL ||
6666
tb[NFTA_EXTHDR_OFFSET] == NULL ||
6767
tb[NFTA_EXTHDR_LEN] == NULL)
6868
return -EINVAL;
6969

70-
offset = ntohl(nla_get_be32(tb[NFTA_EXTHDR_OFFSET]));
71-
len = ntohl(nla_get_be32(tb[NFTA_EXTHDR_LEN]));
70+
err = nft_parse_u32_check(tb[NFTA_EXTHDR_OFFSET], U8_MAX, &offset);
71+
if (err < 0)
72+
return err;
7273

73-
if (offset > U8_MAX || len > U8_MAX)
74-
return -ERANGE;
74+
err = nft_parse_u32_check(tb[NFTA_EXTHDR_LEN], U8_MAX, &len);
75+
if (err < 0)
76+
return err;
7577

7678
priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]);
7779
priv->offset = offset;

net/netfilter/nft_immediate.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ static int nft_immediate_init(const struct nft_ctx *ctx,
5353
tb[NFTA_IMMEDIATE_DATA]);
5454
if (err < 0)
5555
return err;
56+
57+
if (desc.len > U8_MAX)
58+
return -ERANGE;
59+
5660
priv->dlen = desc.len;
5761

5862
priv->dreg = nft_parse_register(tb[NFTA_IMMEDIATE_DREG]);

0 commit comments

Comments
 (0)