diff mbox series

[linux-5.9,1/1] net: netfilter: fix KASAN: slab-out-of-bounds Read in nft_flow_rule_create

Message ID 20201019172532.3906-1-saeed.mirzamohammadi@oracle.com
State New
Headers show
Series [linux-5.9,1/1] net: netfilter: fix KASAN: slab-out-of-bounds Read in nft_flow_rule_create | expand

Commit Message

Saeed Mirzamohammadi Oct. 19, 2020, 5:25 p.m. UTC
From: Saeed Mirzamohammadi <saeed.mirzamohammadi@oracle.com>

This patch fixes the issue due to:

BUG: KASAN: slab-out-of-bounds in nft_flow_rule_create+0x622/0x6a2
net/netfilter/nf_tables_offload.c:40
Read of size 8 at addr ffff888103910b58 by task syz-executor227/16244

The error happens when expr->ops is accessed early on before performing the boundary check and after nft_expr_next() moves the expr to go out-of-bounds.

This patch checks the boundary condition before expr->ops that fixes the slab-out-of-bounds Read issue.

Signed-off-by: Saeed Mirzamohammadi <saeed.mirzamohammadi@oracle.com>
---
 net/netfilter/nf_tables_offload.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Pablo Neira Ayuso Oct. 20, 2020, 11:50 a.m. UTC | #1
On Mon, Oct 19, 2020 at 10:25:32AM -0700, saeed.mirzamohammadi@oracle.com wrote:
> From: Saeed Mirzamohammadi <saeed.mirzamohammadi@oracle.com>

> 

> This patch fixes the issue due to:

> 

> BUG: KASAN: slab-out-of-bounds in nft_flow_rule_create+0x622/0x6a2

> net/netfilter/nf_tables_offload.c:40

> Read of size 8 at addr ffff888103910b58 by task syz-executor227/16244

> 

> The error happens when expr->ops is accessed early on before performing the boundary check and after nft_expr_next() moves the expr to go out-of-bounds.

> 

> This patch checks the boundary condition before expr->ops that fixes the slab-out-of-bounds Read issue.


Thanks. I made a slight variant of your patch.

I'm attaching it, it is also fixing the problem but it introduced
nft_expr_more() and use it everywhere.

Let me know if this looks fine to you.
From 3f60e5f489ec44e8b0a7e9e622c93be4df335fb6 Mon Sep 17 00:00:00 2001
From: Saeed Mirzamohammadi <saeed.mirzamohammadi@oracle.com>

Date: Tue, 20 Oct 2020 13:41:36 +0200
Subject: [PATCH nf] netfilter: fix KASAN: slab-out-of-bounds Read in
 nft_flow_rule_create

This patch fixes the issue due to:

BUG: KASAN: slab-out-of-bounds in nft_flow_rule_create+0x622/0x6a2
net/netfilter/nf_tables_offload.c:40
Read of size 8 at addr ffff888103910b58 by task syz-executor227/16244

The error happens when expr->ops is accessed early on before performing the boundary check and after nft_expr_next() moves the expr to go out-of-bounds.

This patch checks the boundary condition before expr->ops that fixes the slab-out-of-bounds Read issue.

Add nft_expr_more() and use it to fix this problem.

Signed-off-by: Saeed Mirzamohammadi <saeed.mirzamohammadi@oracle.com>

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>

---
 include/net/netfilter/nf_tables.h | 6 ++++++
 net/netfilter/nf_tables_api.c     | 6 +++---
 net/netfilter/nf_tables_offload.c | 4 ++--
 3 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/include/net/netfilter/nf_tables.h b/include/net/netfilter/nf_tables.h
index 3f7e56b1171e..55b4cadf290a 100644
--- a/include/net/netfilter/nf_tables.h
+++ b/include/net/netfilter/nf_tables.h
@@ -891,6 +891,12 @@ static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
 	return (struct nft_expr *)&rule->data[rule->dlen];
 }
 
+static inline bool nft_expr_more(const struct nft_rule *rule,
+				 const struct nft_expr *expr)
+{
+	return expr != nft_expr_last(rule) && expr->ops;
+}
+
 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
 {
 	return (void *)&rule->data[rule->dlen];
diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
index 9957e0ed8658..65cb8e3c13d9 100644
--- a/net/netfilter/nf_tables_api.c
+++ b/net/netfilter/nf_tables_api.c
@@ -302,7 +302,7 @@ static void nft_rule_expr_activate(const struct nft_ctx *ctx,
 	struct nft_expr *expr;
 
 	expr = nft_expr_first(rule);
-	while (expr != nft_expr_last(rule) && expr->ops) {
+	while (nft_expr_more(rule, expr)) {
 		if (expr->ops->activate)
 			expr->ops->activate(ctx, expr);
 
@@ -317,7 +317,7 @@ static void nft_rule_expr_deactivate(const struct nft_ctx *ctx,
 	struct nft_expr *expr;
 
 	expr = nft_expr_first(rule);
-	while (expr != nft_expr_last(rule) && expr->ops) {
+	while (nft_expr_more(rule, expr)) {
 		if (expr->ops->deactivate)
 			expr->ops->deactivate(ctx, expr, phase);
 
@@ -3080,7 +3080,7 @@ static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
 	 * is called on error from nf_tables_newrule().
 	 */
 	expr = nft_expr_first(rule);
-	while (expr != nft_expr_last(rule) && expr->ops) {
+	while (nft_expr_more(rule, expr)) {
 		next = nft_expr_next(expr);
 		nf_tables_expr_destroy(ctx, expr);
 		expr = next;
diff --git a/net/netfilter/nf_tables_offload.c b/net/netfilter/nf_tables_offload.c
index 7c7e06624dc3..9f625724a20f 100644
--- a/net/netfilter/nf_tables_offload.c
+++ b/net/netfilter/nf_tables_offload.c
@@ -37,7 +37,7 @@ struct nft_flow_rule *nft_flow_rule_create(struct net *net,
 	struct nft_expr *expr;
 
 	expr = nft_expr_first(rule);
-	while (expr->ops && expr != nft_expr_last(rule)) {
+	while (nft_expr_more(rule, expr)) {
 		if (expr->ops->offload_flags & NFT_OFFLOAD_F_ACTION)
 			num_actions++;
 
@@ -61,7 +61,7 @@ struct nft_flow_rule *nft_flow_rule_create(struct net *net,
 	ctx->net = net;
 	ctx->dep.type = NFT_OFFLOAD_DEP_UNSPEC;
 
-	while (expr->ops && expr != nft_expr_last(rule)) {
+	while (nft_expr_more(rule, expr)) {
 		if (!expr->ops->offload) {
 			err = -EOPNOTSUPP;
 			goto err_out;
-- 
2.20.1
Saeed Mirzamohammadi Oct. 20, 2020, 4:45 p.m. UTC | #2
Thanks! Yes, that looks good to me.

Saeed

> On Oct 20, 2020, at 4:50 AM, Pablo Neira Ayuso <pablo@netfilter.org> wrote:

> 

> On Mon, Oct 19, 2020 at 10:25:32AM -0700, saeed.mirzamohammadi@oracle.com wrote:

>> From: Saeed Mirzamohammadi <saeed.mirzamohammadi@oracle.com>

>> 

>> This patch fixes the issue due to:

>> 

>> BUG: KASAN: slab-out-of-bounds in nft_flow_rule_create+0x622/0x6a2

>> net/netfilter/nf_tables_offload.c:40

>> Read of size 8 at addr ffff888103910b58 by task syz-executor227/16244

>> 

>> The error happens when expr->ops is accessed early on before performing the boundary check and after nft_expr_next() moves the expr to go out-of-bounds.

>> 

>> This patch checks the boundary condition before expr->ops that fixes the slab-out-of-bounds Read issue.

> 

> Thanks. I made a slight variant of your patch.

> 

> I'm attaching it, it is also fixing the problem but it introduced

> nft_expr_more() and use it everywhere.

> 

> Let me know if this looks fine to you.

> <0001-netfilter-fix-KASAN-slab-out-of-bounds-Read-in-nft_f.patch>
Saeed Mirzamohammadi Oct. 21, 2020, 8:08 p.m. UTC | #3
Attached the syzkaller C repro.

Tested-by: Saeed Mirzamohammadi <saeed.mirzamohammadi@oracle.com>
> On Oct 20, 2020, at 9:45 AM, Saeed Mirzamohammadi <saeed.mirzamohammadi@oracle.com> wrote:

> 

> Thanks! Yes, that looks good to me.

> 

> Saeed

> 

>> On Oct 20, 2020, at 4:50 AM, Pablo Neira Ayuso <pablo@netfilter.org> wrote:

>> 

>> On Mon, Oct 19, 2020 at 10:25:32AM -0700, saeed.mirzamohammadi@oracle.com wrote:

>>> From: Saeed Mirzamohammadi <saeed.mirzamohammadi@oracle.com>

>>> 

>>> This patch fixes the issue due to:

>>> 

>>> BUG: KASAN: slab-out-of-bounds in nft_flow_rule_create+0x622/0x6a2

>>> net/netfilter/nf_tables_offload.c:40

>>> Read of size 8 at addr ffff888103910b58 by task syz-executor227/16244

>>> 

>>> The error happens when expr->ops is accessed early on before performing the boundary check and after nft_expr_next() moves the expr to go out-of-bounds.

>>> 

>>> This patch checks the boundary condition before expr->ops that fixes the slab-out-of-bounds Read issue.

>> 

>> Thanks. I made a slight variant of your patch.

>> 

>> I'm attaching it, it is also fixing the problem but it introduced

>> nft_expr_more() and use it everywhere.

>> 

>> Let me know if this looks fine to you.

>> <0001-netfilter-fix-KASAN-slab-out-of-bounds-Read-in-nft_f.patch>

>
Greg Kroah-Hartman Oct. 27, 2020, 6:21 a.m. UTC | #4
On Sun, Oct 25, 2020 at 04:31:57PM -0700, Saeed Mirzamohammadi wrote:
> Adding stable.


What did that do?

confused,

greg k-h
Greg Kroah-Hartman Oct. 29, 2020, 11:02 a.m. UTC | #5
On Tue, Oct 27, 2020 at 09:19:22AM +0100, Pablo Neira Ayuso wrote:
> Hi Greg,
> 
> On Tue, Oct 27, 2020 at 07:21:11AM +0100, Greg KH wrote:
> > On Sun, Oct 25, 2020 at 04:31:57PM -0700, Saeed Mirzamohammadi wrote:
> > > Adding stable.
> > 
> > What did that do?
> 
> Saeed is requesting that stable maintainers cherry-picks this patch:
> 
> 31cc578ae2de ("netfilter: nftables_offload: KASAN slab-out-of-bounds
> Read in nft_flow_rule_create")
> 
> into stable 5.4 and 5.8.

5.9 is also a stable kernel :)

Will go queue it up everywhere...

thanks,

greg k-h
Pablo Neira Ayuso Oct. 29, 2020, 11:06 a.m. UTC | #6
On Thu, Oct 29, 2020 at 12:02:41PM +0100, Greg KH wrote:
> On Tue, Oct 27, 2020 at 09:19:22AM +0100, Pablo Neira Ayuso wrote:
> > Hi Greg,
> > 
> > On Tue, Oct 27, 2020 at 07:21:11AM +0100, Greg KH wrote:
> > > On Sun, Oct 25, 2020 at 04:31:57PM -0700, Saeed Mirzamohammadi wrote:
> > > > Adding stable.
> > > 
> > > What did that do?
> > 
> > Saeed is requesting that stable maintainers cherry-picks this patch:
> > 
> > 31cc578ae2de ("netfilter: nftables_offload: KASAN slab-out-of-bounds
> > Read in nft_flow_rule_create")
> > 
> > into stable 5.4 and 5.8.
> 
> 5.9 is also a stable kernel :)

Oh, indeed, I forgot this one :)

> Will go queue it up everywhere...

Thanks.
diff mbox series

Patch

diff --git a/net/netfilter/nf_tables_offload.c b/net/netfilter/nf_tables_offload.c
index 9ef37c1b7b3b..1273e3c0d4b8 100644
--- a/net/netfilter/nf_tables_offload.c
+++ b/net/netfilter/nf_tables_offload.c
@@ -37,7 +37,7 @@  struct nft_flow_rule *nft_flow_rule_create(struct net *net,
 	struct nft_expr *expr;
 
 	expr = nft_expr_first(rule);
-	while (expr->ops && expr != nft_expr_last(rule)) {
+	while (expr != nft_expr_last(rule) && expr->ops) {
 		if (expr->ops->offload_flags & NFT_OFFLOAD_F_ACTION)
 			num_actions++;
 
@@ -61,7 +61,7 @@  struct nft_flow_rule *nft_flow_rule_create(struct net *net,
 	ctx->net = net;
 	ctx->dep.type = NFT_OFFLOAD_DEP_UNSPEC;
 
-	while (expr->ops && expr != nft_expr_last(rule)) {
+	while (expr != nft_expr_last(rule) && expr->ops) {
 		if (!expr->ops->offload) {
 			err = -EOPNOTSUPP;
 			goto err_out;