netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] netfilter: nf_tables: prevent shift wrap in nft_chain_parse_hook()
@ 2019-04-02 13:30 Dan Carpenter
  2019-04-05  8:34 ` Dan Carpenter
  2019-04-06  5:26 ` [PATCH v2] " Dan Carpenter
  0 siblings, 2 replies; 5+ messages in thread
From: Dan Carpenter @ 2019-04-02 13:30 UTC (permalink / raw)
  To: Pablo Neira Ayuso
  Cc: Jozsef Kadlecsik, Florian Westphal, netfilter-devel, coreteam,
	kernel-janitors

I believe that "hook->num" can be up to UINT_MAX so the shift has to
be unsigned or shifting more than 31 is undefined.  It's possible that
this leads to array overflow in nf_tables_addchain():

	ops->hook	= hook.type->hooks[ops->hooknum];

Fixes: fe19c04ca137 ("netfilter: nf_tables: remove nhooks field from struct nft_af_info")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
 net/netfilter/nf_tables_api.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
index ef7772e976cc..b6e48fe7c776 100644
--- a/net/netfilter/nf_tables_api.c
+++ b/net/netfilter/nf_tables_api.c
@@ -1545,7 +1545,7 @@ static int nft_chain_parse_hook(struct net *net,
 		if (IS_ERR(type))
 			return PTR_ERR(type);
 	}
-	if (!(type->hook_mask & (1 << hook->num)))
+	if (!(type->hook_mask & (1U << hook->num)))
 		return -EOPNOTSUPP;
 
 	if (type->type == NFT_CHAIN_T_NAT &&
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] netfilter: nf_tables: prevent shift wrap in nft_chain_parse_hook()
  2019-04-02 13:30 [PATCH] netfilter: nf_tables: prevent shift wrap in nft_chain_parse_hook() Dan Carpenter
@ 2019-04-05  8:34 ` Dan Carpenter
  2019-04-05 10:01   ` Florian Westphal
  2019-04-06  5:26 ` [PATCH v2] " Dan Carpenter
  1 sibling, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2019-04-05  8:34 UTC (permalink / raw)
  To: Pablo Neira Ayuso
  Cc: Jozsef Kadlecsik, Florian Westphal, netfilter-devel, coreteam,
	kernel-janitors

On Tue, Apr 02, 2019 at 04:30:38PM +0300, Dan Carpenter wrote:
> I believe that "hook->num" can be up to UINT_MAX so the shift has to
> be unsigned or shifting more than 31 is undefined.  It's possible that
> this leads to array overflow in nf_tables_addchain():
> 
> 	ops->hook	= hook.type->hooks[ops->hooknum];
> 
> Fixes: fe19c04ca137 ("netfilter: nf_tables: remove nhooks field from struct nft_af_info")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
>  net/netfilter/nf_tables_api.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
> index ef7772e976cc..b6e48fe7c776 100644
> --- a/net/netfilter/nf_tables_api.c
> +++ b/net/netfilter/nf_tables_api.c
> @@ -1545,7 +1545,7 @@ static int nft_chain_parse_hook(struct net *net,
>  		if (IS_ERR(type))
>  			return PTR_ERR(type);
>  	}
> -	if (!(type->hook_mask & (1 << hook->num)))
> +	if (!(type->hook_mask & (1U << hook->num)))

Ugh...  No this doesn't work.  It's still undefined behavoir even if we
make it unsigned.  It has to be:

	if (hook->num > 31 || !(type->hook_mask & (1 << hook->num)))

which seems like a slightly ugly magic number but it fixes the buffer
overflow.

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] netfilter: nf_tables: prevent shift wrap in nft_chain_parse_hook()
  2019-04-05  8:34 ` Dan Carpenter
@ 2019-04-05 10:01   ` Florian Westphal
  0 siblings, 0 replies; 5+ messages in thread
From: Florian Westphal @ 2019-04-05 10:01 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Pablo Neira Ayuso, Jozsef Kadlecsik, Florian Westphal,
	netfilter-devel, coreteam, kernel-janitors

Dan Carpenter <dan.carpenter@oracle.com> wrote:
> 	if (hook->num > 31 || !(type->hook_mask & (1 << hook->num)))
> 
> which seems like a slightly ugly magic number but it fixes the buffer
> overflow.

You can use NF_MAX_HOOKS instead of 31.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] netfilter: nf_tables: prevent shift wrap in nft_chain_parse_hook()
  2019-04-02 13:30 [PATCH] netfilter: nf_tables: prevent shift wrap in nft_chain_parse_hook() Dan Carpenter
  2019-04-05  8:34 ` Dan Carpenter
@ 2019-04-06  5:26 ` Dan Carpenter
  2019-04-13 12:58   ` Pablo Neira Ayuso
  1 sibling, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2019-04-06  5:26 UTC (permalink / raw)
  To: Pablo Neira Ayuso
  Cc: Jozsef Kadlecsik, Florian Westphal, netfilter-devel, coreteam,
	kernel-janitors

I believe that "hook->num" can be up to UINT_MAX.  Shifting more than
31 bits would is undefined in C but in practice it would lead to shift
wrapping.  That would lead to an array overflow in nf_tables_addchain():

	ops->hook       = hook.type->hooks[ops->hooknum];

Fixes: fe19c04ca137 ("netfilter: nf_tables: remove nhooks field from struct nft_af_info")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
v2:  In my original fix I thought making shifts unsigned was defined in
C but it's not.  My original patch didn't fix or change runtime at all.

Thanks to Florian Westphal for his help on this.

 net/netfilter/nf_tables_api.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
index ef7772e976cc..1606eaa5ae0d 100644
--- a/net/netfilter/nf_tables_api.c
+++ b/net/netfilter/nf_tables_api.c
@@ -1545,7 +1545,7 @@ static int nft_chain_parse_hook(struct net *net,
 		if (IS_ERR(type))
 			return PTR_ERR(type);
 	}
-	if (!(type->hook_mask & (1 << hook->num)))
+	if (hook->num > NF_MAX_HOOKS || !(type->hook_mask & (1 << hook->num)))
 		return -EOPNOTSUPP;
 
 	if (type->type == NFT_CHAIN_T_NAT &&
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] netfilter: nf_tables: prevent shift wrap in nft_chain_parse_hook()
  2019-04-06  5:26 ` [PATCH v2] " Dan Carpenter
@ 2019-04-13 12:58   ` Pablo Neira Ayuso
  0 siblings, 0 replies; 5+ messages in thread
From: Pablo Neira Ayuso @ 2019-04-13 12:58 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Jozsef Kadlecsik, Florian Westphal, netfilter-devel, coreteam,
	kernel-janitors

On Sat, Apr 06, 2019 at 08:26:52AM +0300, Dan Carpenter wrote:
> I believe that "hook->num" can be up to UINT_MAX.  Shifting more than
> 31 bits would is undefined in C but in practice it would lead to shift
> wrapping.  That would lead to an array overflow in nf_tables_addchain():
> 
> 	ops->hook       = hook.type->hooks[ops->hooknum];

Applied, thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-04-13 12:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-04-02 13:30 [PATCH] netfilter: nf_tables: prevent shift wrap in nft_chain_parse_hook() Dan Carpenter
2019-04-05  8:34 ` Dan Carpenter
2019-04-05 10:01   ` Florian Westphal
2019-04-06  5:26 ` [PATCH v2] " Dan Carpenter
2019-04-13 12:58   ` Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).