Skip to content

Commit fd89b23

Browse files
Liping Zhangummakynes
authored andcommitted
netfilter: nft_set_bitmap: fetch the element key based on the set->klen
Currently we just assume the element key as a u32 integer, regardless of the set key length. This is incorrect, for example, the tcp port number is only 16 bits. So when we use the nft_payload expr to get the tcp dport and store it to dreg, the dport will be stored at 0~15 bits, and 16~31 bits will be padded with zero. So the reg->data[dreg] will be looked like as below: 0 15 31 +-+-+-+-+-+-+-+-+-+-+-+-+ | tcp dport | 0 | +-+-+-+-+-+-+-+-+-+-+-+-+ But for these big-endian systems, if we treate this register as a u32 integer, the element key will be larger than 65535, so the following lookup in bitmap set will cause out of bound access. Another issue is that if we add element with comments in bitmap set(although the comments will be ignored eventually), the element will vanish strangely. Because we treate the element key as a u32 integer, so the comments will become the part of the element key, then the element key will also be larger than 65535 and out of bound access will happen: # nft add element t s { 1 comment test } Since set->klen is 1 or 2, it's fine to treate the element key as a u8 or u16 integer. Fixes: 665153f ("netfilter: nf_tables: add bitmap set type") Signed-off-by: Liping Zhang <zlpnobody@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 568af6d commit fd89b23

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

‎net/netfilter/nft_set_bitmap.c‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,17 @@ struct nft_bitmap {
4545
u8 bitmap[];
4646
};
4747

48-
static inline void nft_bitmap_location(u32 key, u32 *idx, u32 *off)
48+
static inline void nft_bitmap_location(const struct nft_set *set,
49+
const void *key,
50+
u32 *idx, u32 *off)
4951
{
50-
u32 k = (key << 1);
52+
u32 k;
53+
54+
if (set->klen == 2)
55+
k = *(u16 *)key;
56+
else
57+
k = *(u8 *)key;
58+
k <<= 1;
5159

5260
*idx = k / BITS_PER_BYTE;
5361
*off = k % BITS_PER_BYTE;
@@ -69,7 +77,7 @@ static bool nft_bitmap_lookup(const struct net *net, const struct nft_set *set,
6977
u8 genmask = nft_genmask_cur(net);
7078
u32 idx, off;
7179

72-
nft_bitmap_location(*key, &idx, &off);
80+
nft_bitmap_location(set, key, &idx, &off);
7381

7482
return nft_bitmap_active(priv->bitmap, idx, off, genmask);
7583
}
@@ -83,7 +91,7 @@ static int nft_bitmap_insert(const struct net *net, const struct nft_set *set,
8391
u8 genmask = nft_genmask_next(net);
8492
u32 idx, off;
8593

86-
nft_bitmap_location(nft_set_ext_key(ext)->data[0], &idx, &off);
94+
nft_bitmap_location(set, nft_set_ext_key(ext), &idx, &off);
8795
if (nft_bitmap_active(priv->bitmap, idx, off, genmask))
8896
return -EEXIST;
8997

@@ -102,7 +110,7 @@ static void nft_bitmap_remove(const struct net *net,
102110
u8 genmask = nft_genmask_next(net);
103111
u32 idx, off;
104112

105-
nft_bitmap_location(nft_set_ext_key(ext)->data[0], &idx, &off);
113+
nft_bitmap_location(set, nft_set_ext_key(ext), &idx, &off);
106114
/* Enter 00 state. */
107115
priv->bitmap[idx] &= ~(genmask << off);
108116
}
@@ -116,7 +124,7 @@ static void nft_bitmap_activate(const struct net *net,
116124
u8 genmask = nft_genmask_next(net);
117125
u32 idx, off;
118126

119-
nft_bitmap_location(nft_set_ext_key(ext)->data[0], &idx, &off);
127+
nft_bitmap_location(set, nft_set_ext_key(ext), &idx, &off);
120128
/* Enter 11 state. */
121129
priv->bitmap[idx] |= (genmask << off);
122130
}
@@ -128,7 +136,7 @@ static bool nft_bitmap_flush(const struct net *net,
128136
u8 genmask = nft_genmask_next(net);
129137
u32 idx, off;
130138

131-
nft_bitmap_location(nft_set_ext_key(ext)->data[0], &idx, &off);
139+
nft_bitmap_location(set, nft_set_ext_key(ext), &idx, &off);
132140
/* Enter 10 state, similar to deactivation. */
133141
priv->bitmap[idx] &= ~(genmask << off);
134142

@@ -161,10 +169,9 @@ static void *nft_bitmap_deactivate(const struct net *net,
161169
struct nft_bitmap *priv = nft_set_priv(set);
162170
u8 genmask = nft_genmask_next(net);
163171
struct nft_set_ext *ext;
164-
u32 idx, off, key = 0;
172+
u32 idx, off;
165173

166-
memcpy(&key, elem->key.val.data, set->klen);
167-
nft_bitmap_location(key, &idx, &off);
174+
nft_bitmap_location(set, elem->key.val.data, &idx, &off);
168175

169176
if (!nft_bitmap_active(priv->bitmap, idx, off, genmask))
170177
return NULL;

0 commit comments

Comments
 (0)