2021-03-11 03:18:10

by Lv Yunlong

[permalink] [raw]
Subject: [PATCH] infiniband/i40iw: Fix a use after free in i40iw_cm_event_handler

In the case of I40IW_CM_EVENT_ABORTED, i40iw_event_connect_error()
could be called to free the event->cm_node. However, event->cm_node
will be used after and cause use after free. It needs to add flags
to inform that event->cm_node has been freed.

Signed-off-by: Lv Yunlong <[email protected]>
---
drivers/infiniband/hw/i40iw/i40iw_cm.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/infiniband/hw/i40iw/i40iw_cm.c b/drivers/infiniband/hw/i40iw/i40iw_cm.c
index ac65c8237b2e..447b43c2d21f 100644
--- a/drivers/infiniband/hw/i40iw/i40iw_cm.c
+++ b/drivers/infiniband/hw/i40iw/i40iw_cm.c
@@ -4175,6 +4175,7 @@ static void i40iw_cm_event_handler(struct work_struct *work)
struct i40iw_cm_event,
event_work);
struct i40iw_cm_node *cm_node;
+ int flags = 0;

if (!event || !event->cm_node || !event->cm_node->cm_core)
return;
@@ -4211,6 +4212,7 @@ static void i40iw_cm_event_handler(struct work_struct *work)
(event->cm_node->state == I40IW_CM_STATE_OFFLOADED))
break;
i40iw_event_connect_error(event);
+ flags = 1;
break;
default:
i40iw_pr_err("event type = %d\n", event->type);
@@ -4218,7 +4220,8 @@ static void i40iw_cm_event_handler(struct work_struct *work)
}

event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
- i40iw_rem_ref_cm_node(event->cm_node);
+ if (!flags)
+ i40iw_rem_ref_cm_node(event->cm_node);
kfree(event);
}

--
2.25.1



2021-03-11 18:23:50

by Jason Gunthorpe

[permalink] [raw]
Subject: Re: [PATCH] infiniband/i40iw: Fix a use after free in i40iw_cm_event_handler

On Wed, Mar 10, 2021 at 07:14:14PM -0800, Lv Yunlong wrote:
> In the case of I40IW_CM_EVENT_ABORTED, i40iw_event_connect_error()
> could be called to free the event->cm_node. However, event->cm_node
> will be used after and cause use after free. It needs to add flags
> to inform that event->cm_node has been freed.
>
> Signed-off-by: Lv Yunlong <[email protected]>
> ---
> drivers/infiniband/hw/i40iw/i40iw_cm.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)

This might be OK (though I don't like the free variable), Shiraz??

Jason

2021-03-12 01:15:24

by Shiraz Saleem

[permalink] [raw]
Subject: RE: [PATCH] infiniband/i40iw: Fix a use after free in i40iw_cm_event_handler

> Subject: Re: [PATCH] infiniband/i40iw: Fix a use after free in
> i40iw_cm_event_handler
>
> On Wed, Mar 10, 2021 at 07:14:14PM -0800, Lv Yunlong wrote:
> > In the case of I40IW_CM_EVENT_ABORTED, i40iw_event_connect_error()
> > could be called to free the event->cm_node. However, event->cm_node
> > will be used after and cause use after free. It needs to add flags to
> > inform that event->cm_node has been freed.
> >
> > Signed-off-by: Lv Yunlong <[email protected]>
> > ---
> > drivers/infiniband/hw/i40iw/i40iw_cm.c | 5 ++++-
> > 1 file changed, 4 insertions(+), 1 deletion(-)
>
> This might be OK (though I don't like the free variable), Shiraz??
>

How was this reproduced? Do you have some call trace leading up to use after free?

The cm_node refcnt is bumped at creation time and once in i40iw_receive_ilq before packet is processed.
That should protect the cm_node from disappearing in the event handler in the abort event case.
The dec at end of i40iw_receive ilq should be point where the cm_node is freed specifically in the abort case.

Shiraz

2021-03-12 01:59:19

by Lv Yunlong

[permalink] [raw]
Subject: Re: RE: [PATCH] infiniband/i40iw: Fix a use after free in i40iw_cm_event_handler




> -----原始邮件-----
> 发件人: "Saleem, Shiraz" <[email protected]>
> 发送时间: 2021-03-12 09:13:39 (星期五)
> 收件人: "Jason Gunthorpe" <[email protected]>, "Lv Yunlong" <[email protected]>
> 抄送: "Latif, Faisal" <[email protected]>, "[email protected]" <[email protected]>, "[email protected]" <[email protected]>, "[email protected]" <[email protected]>
> 主题: RE: [PATCH] infiniband/i40iw: Fix a use after free in i40iw_cm_event_handler
>
> > Subject: Re: [PATCH] infiniband/i40iw: Fix a use after free in
> > i40iw_cm_event_handler
> >
> > On Wed, Mar 10, 2021 at 07:14:14PM -0800, Lv Yunlong wrote:
> > > In the case of I40IW_CM_EVENT_ABORTED, i40iw_event_connect_error()
> > > could be called to free the event->cm_node. However, event->cm_node
> > > will be used after and cause use after free. It needs to add flags to
> > > inform that event->cm_node has been freed.
> > >
> > > Signed-off-by: Lv Yunlong <[email protected]>
> > > ---
> > > drivers/infiniband/hw/i40iw/i40iw_cm.c | 5 ++++-
> > > 1 file changed, 4 insertions(+), 1 deletion(-)
> >
> > This might be OK (though I don't like the free variable), Shiraz??
> >
>
> How was this reproduced? Do you have some call trace leading up to use after free?
>
> The cm_node refcnt is bumped at creation time and once in i40iw_receive_ilq before packet is processed.
> That should protect the cm_node from disappearing in the event handler in the abort event case.
> The dec at end of i40iw_receive ilq should be point where the cm_node is freed specifically in the abort case.
>
> Shiraz
>

This problem was reported by a path-sensitive analyzer developed by our Security Lab(Loccs).
The analyzer reported that there is a feasible path to free event->cm_node and use it after,
and that is what i described in the first commit.

Thanks.

2021-03-13 01:13:59

by Shiraz Saleem

[permalink] [raw]
Subject: RE: RE: [PATCH] infiniband/i40iw: Fix a use after free in i40iw_cm_event_handler

> Subject: Re: RE: [PATCH] infiniband/i40iw: Fix a use after free in
> i40iw_cm_event_handler
>
>
>
>
> > -----原始邮件-----
> > 发件人: "Saleem, Shiraz" <[email protected]>
> > 发送时间: 2021-03-12 09:13:39 (星期五)
> > 收件人: "Jason Gunthorpe" <[email protected]>, "Lv Yunlong"
> > <[email protected]>
> > 抄送: "Latif, Faisal" <[email protected]>, "[email protected]"
> > <[email protected]>, "[email protected]"
> > <[email protected]>, "[email protected]"
> > <[email protected]>
> > 主题: RE: [PATCH] infiniband/i40iw: Fix a use after free in
> > i40iw_cm_event_handler
> >
> > > Subject: Re: [PATCH] infiniband/i40iw: Fix a use after free in
> > > i40iw_cm_event_handler
> > >
> > > On Wed, Mar 10, 2021 at 07:14:14PM -0800, Lv Yunlong wrote:
> > > > In the case of I40IW_CM_EVENT_ABORTED, i40iw_event_connect_error()
> > > > could be called to free the event->cm_node. However,
> > > > event->cm_node will be used after and cause use after free. It
> > > > needs to add flags to inform that event->cm_node has been freed.
> > > >
> > > > Signed-off-by: Lv Yunlong <[email protected]>
> > > > ---
> > > > drivers/infiniband/hw/i40iw/i40iw_cm.c | 5 ++++-
> > > > 1 file changed, 4 insertions(+), 1 deletion(-)
> > >
> > > This might be OK (though I don't like the free variable), Shiraz??
> > >
> >
> > How was this reproduced? Do you have some call trace leading up to use after
> free?
> >
> > The cm_node refcnt is bumped at creation time and once in i40iw_receive_ilq
> before packet is processed.
> > That should protect the cm_node from disappearing in the event handler in the
> abort event case.
> > The dec at end of i40iw_receive ilq should be point where the cm_node is freed
> specifically in the abort case.
> >
> > Shiraz
> >
>
> This problem was reported by a path-sensitive analyzer developed by our Security
> Lab(Loccs).
> The analyzer reported that there is a feasible path to free event->cm_node and use
> it after, and that is what i described in the first commit.
>

OK. I don’t think that an extra dec refcnt in itself for the abort case is enough evidence to
say that the cm_node is freed in i40iw_event_connect_error.

Shiraz