linux-rdma.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Race condition between cm_migrate() and cm_remove_one()
@ 2020-11-04 22:08 Ryan Stone
  2020-11-12 19:46 ` Jason Gunthorpe
  0 siblings, 1 reply; 2+ messages in thread
From: Ryan Stone @ 2020-11-04 22:08 UTC (permalink / raw)
  To: linux-rdma

I believe that there is a serious bug in drivers/infiniband/core/cm.c
that can cause kernel memory corruption.  The problem arises from the
interaction between cm_remove_one() and this code in cm_migrate():

https://code.woboq.org/linux/linux/drivers/infiniband/core/cm.c.html#3968

As I understand it, this code swaps the values of
prim_send_port_not_ready and altr_send_port_not_ready when we fail
over to the alternate path.

In cm_remove_one(), prim_send_port_not_ready and
altr_send_port_not_ready are set to true for every cm_id_priv
associated with the client being removed:

https://code.woboq.org/linux/linux/drivers/infiniband/core/cm.c.html#4478

I see two problems here: first, the two functions hold different
locks.  This means that we can lose a write to
prim_send_port_not_ready or altr_send_port_not_ready if it happens
concurrently with cm_migrate().

Second, while we swap the values of prim_send_port_not_ready and
altr_send_port_not_ready, the associated cm_priv_prim_list and
cm_priv_altr_list lists are *not* swapped.  I believe that either bug
can cause a subsequent call to cm_destroy_id() to write to the
cm_device that cm_remove_one() has already freed, potentially
corrupting kernel memory.  The issue is the two list_del() calls here:

https://code.woboq.org/linux/linux/drivers/infiniband/core/cm.c.html#1103

If we lose the race with cm_migrate(), then then the list_del() call
can happen when it should not have.  Alternatively, if cm_migrate()
has been invoked an odd number of times for the cm_id_priv and only
one of the prim port/altr port has been set to not ready, then
list_del() will be called on the wrong list.

I'm not very familiar with this code so I'm unsure of the best way to
fix it.  One approach would be:

1) Hold cm.lock in cm_migrate() in addition to the cm_id_priv's lock, and
2) Also swap the contents of the two lists in cm_migrate()

If anybody could give input on my analysis and the proposed solution
I'd really appreciate it.

Thanks,
Ryan

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

* Re: Race condition between cm_migrate() and cm_remove_one()
  2020-11-04 22:08 Race condition between cm_migrate() and cm_remove_one() Ryan Stone
@ 2020-11-12 19:46 ` Jason Gunthorpe
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Gunthorpe @ 2020-11-12 19:46 UTC (permalink / raw)
  To: Ryan Stone; +Cc: linux-rdma

On Wed, Nov 04, 2020 at 05:08:59PM -0500, Ryan Stone wrote:

> If anybody could give input on my analysis and the proposed solution
> I'd really appreciate it.

Yikes, this whole thing is just wrong..

1) We can't migrate QP's across devices. So av and alt_av must be in the
   same cm_dev, we never check this when forming the AV and alt AV's
   during LAP. Wee

2) cm_remove_one needs to remove all the cm_dev's because it is going
   to kfree them. Using altr_send_port_not_ready is foolish because
   what we really want is to NULL the port pointer (we are freeing
   that too)

3) Touching the AV after cm_remove_one(), eg for
   rdma_destroy_ah_attr() is wrong. The AV is part of the cm_dev and
   has to be cleaned up before the cm_remove_one can return.

4) The flush_workqueue() in cm_remove_one is wishful thinking, there
   are many places still using the mad_agent that are not on that
   workqueue.

   A proper 'av_lock' rwsem going to be needed here

   Which is another example of why every time I see some idiodic
   'is_closed' flag it is just a sign of wrong, wrong, wrong.

Fixing it requires a full audit of all the places using the AV :\

Jason

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

end of thread, other threads:[~2020-11-12 19:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-04 22:08 Race condition between cm_migrate() and cm_remove_one() Ryan Stone
2020-11-12 19:46 ` Jason Gunthorpe

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).