netfilter: nf_tables: replace BUG_ON by element length check
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 1/1 netfilter: nf_tables: replace BUG_ON by element length check
BUG_ON can be triggered from userspace with a combination of sufficiently large extensions. Replace it by length check and return EINVAL instead.
Fixes 3ac4c07a2400 ("netfilter: nf_tables: add set extensions")
Signed-off-by Pablo Neira Ayuso <[email protected]>
---
Extensions have been growing over time, let's replace this BUG_ON by EINVAL.
include/net/netfilter/nf_tables.h | 14 +++++++---- net/netfilter/nf_tables_api.c | 39 +++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 17 deletions(-)
include/net/netfilter/nf_tables.h +9 -5
@@ -657,18 +657,22 @@ static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl) tmpl->len =sizeof (struct nft_set_ext ); } -static inline void nft_set_ext_add_length (struct nft_set_ext_tmpl *tmpl,u8 id, -unsigned int len) +static inline int nft_set_ext_add_length (struct nft_set_ext_tmpl *tmpl,u8 id, +unsigned int len) { tmpl->len =ALIGN (tmpl->len, nft_set_ext_types[id].align); -BUG_ON (tmpl->len >U8_MAX ); +if (tmpl->len >U8_MAX ) +return -EINVAL ; + tmpl->offset[id] = tmpl->len; tmpl->len += nft_set_ext_types[id].len + len; + +return 0 ; } -static inline void nft_set_ext_add (struct nft_set_ext_tmpl *tmpl,u8 id) +static inline int nft_set_ext_add (struct nft_set_ext_tmpl *tmpl,u8 id) { -nft_set_ext_add_length (tmpl, id,0 ); +return nft_set_ext_add_length (tmpl, id,0 ); } static inline void nft_set_ext_init(struct nft_set_ext *ext,
net/netfilter/nf_tables_api.c +27 -12
@@ -5943,7 +5943,9 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,if (err <0 ) goto err_set_elem_expr; -nft_set_ext_add_length (&tmpl,NFT_SET_EXT_KEY , set->klen); + err =nft_set_ext_add_length (&tmpl,NFT_SET_EXT_KEY , set->klen); +if (err <0 ) + goto err_parse_key; }if (nla[NFTA_SET_ELEM_KEY_END ]) {
@@ -5952,22 +5954,28 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,if (err <0 ) goto err_parse_key; -nft_set_ext_add_length (&tmpl,NFT_SET_EXT_KEY_END , set->klen); + err =nft_set_ext_add_length (&tmpl,NFT_SET_EXT_KEY_END , set->klen); +if (err <0 ) + goto err_parse_key_end; }if (timeout >0 ) {nft_set_ext_add (&tmpl,NFT_SET_EXT_EXPIRATION ); -if (timeout != set->timeout) -nft_set_ext_add (&tmpl,NFT_SET_EXT_TIMEOUT ); +if (timeout != set->timeout) { + err =nft_set_ext_add (&tmpl,NFT_SET_EXT_TIMEOUT ); +if (err <0 ) + goto err_parse_key_end; + } }if (num_exprs) {for (i =0 ; i < num_exprs; i++) size += expr_array[i]->ops->size; -nft_set_ext_add_length (&tmpl,NFT_SET_EXT_EXPRESSIONS , -sizeof (struct nft_set_elem_expr ) + - size); + err =nft_set_ext_add_length (&tmpl,NFT_SET_EXT_EXPRESSIONS , +sizeof (struct nft_set_elem_expr ) + size); +if (err <0 ) + goto err_parse_key_end; }if (nla[NFTA_SET_ELEM_OBJREF ] !=NULL ) {
@@ -5982,7 +5990,9 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, err =PTR_ERR (obj); goto err_parse_key_end; } -nft_set_ext_add (&tmpl,NFT_SET_EXT_OBJREF ); + err =nft_set_ext_add (&tmpl,NFT_SET_EXT_OBJREF ); +if (err <0 ) + goto err_parse_key_end; }if (nla[NFTA_SET_ELEM_DATA ] !=NULL ) {
@@ -6016,7 +6026,9 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, NFT_VALIDATE_NEED); } -nft_set_ext_add_length (&tmpl,NFT_SET_EXT_DATA , desc.len); + err =nft_set_ext_add_length (&tmpl,NFT_SET_EXT_DATA , desc.len); +if (err <0 ) + goto err_parse_data; }/* The full maximum length of userdata can exceed the maximum
@@ -6026,9 +6038,12 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, ulen =0 ;if (nla[NFTA_SET_ELEM_USERDATA ] !=NULL ) { ulen =nla_len (nla[NFTA_SET_ELEM_USERDATA ]); -if (ulen >0 ) -nft_set_ext_add_length (&tmpl,NFT_SET_EXT_USERDATA , - ulen); +if (ulen >0 ) { + err =nft_set_ext_add_length (&tmpl,NFT_SET_EXT_USERDATA , + ulen); +if (err <0 ) + goto err_parse_data; + } } err = -ENOMEM ;
Signature · 1 line
2.30.2