Skip to contentSkip to conversation

staging: lustre: Fix avoid intensive reconnecting for ko2iblnd patch

PATCH In review Eremin, Dmitry 3 participants 11 messages Jan 16, 2018 – Jan 26, 2018 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#1 lore raw

PATCH 1/1 staging: lustre: Fix avoid intensive reconnecting for ko2iblnd patch

In reply to a message not in the archive

In the original commit 4d99b2581effe115376402e710fbcb1c3c073769 was missed one hunk. Added it now to avoid issue with use after free.

---
 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +2 -1
index 2ebc484..a15a625 100644
@@ -890,7 +890,8 @@ void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
 		atomic_dec(&net->ibn_nconns);
 	}
 
-	kfree(conn);
+	if (free_conn)
+		kfree(conn);
 }
 
 int kiblnd_close_peer_conns_locked(struct kib_peer *peer, int why)
Signature · 13 lines
1.8.3.1


--------------------------------------------------------------------
Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park,
17 Krylatskaya Str., Bldg 4, Moscow 121614,
Russian Federation

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#2 lore raw
Quoted 1 line · Dmitry

In the original commit 4d99b2581effe115376402e710fbcb1c3c073769

Please use the documented way to write this:
4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd")

Quoted 1 line

was missed one hunk. Added it now to avoid issue with use after free.

And I do not understand this commit message at all.

Quoted 11 lines
---
 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +0 -0
index 2ebc484..a15a625 100644

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn) · line 894

Quoted 6 lines
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +2 -1
@@ -890,7 +890,8 @@ void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
 		atomic_dec(&net->ibn_nconns);
 	}
 
-	kfree(conn);
+	if (free_conn)
+		kfree(conn);

This looks really odd, don't you think?

thanks,

greg k-h

#5 lore raw

PATCH 1/1 staging: lustre: Fix avoid intensive reconnecting for ko2iblnd patch

In reply to a message not in the archive

The logic of the original commit 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd") was assumed conditional free of struct kib_conn if the second argument free_conn in function kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn) is true. But this hunk of code was dropped from original commit. As result the logic works wrong and current code use struct kib_conn after free.

Quoted 17 lines

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c
3317 kiblnd_destroy_conn(conn, !peer);
^^^^ Freed always (but should be conditionally)
3318
3319 spin_lock_irqsave(lock, flags);
3320 if (!peer)
3321 continue;
3322

  3323                          conn->ibc_peer = peer;
                                    ^^^^^^^^^^^^^ Use after free
  3324                          if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
  3325                                  list_add_tail(&conn->ibc_list,
                                                                       ^^^^^^^^^^^^
  3326                                                &kiblnd_data.kib_reconn_list);
  3327                          else
  3328                                  list_add_tail(&conn->ibc_list,
                                                                        ^^^^^^^^^^^^
Quoted 1 line

3329 &kiblnd_data.kib_reconn_wait);

After attached patch this code will use struct kib_conn only when it was not freed.

---
 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +2 -1
index 2ebc484..a15a625 100644
@@ -890,7 +890,8 @@ void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
 		atomic_dec(&net->ibn_nconns);
 	}
 
-	kfree(conn);
+	if (free_conn)
+		kfree(conn);
 }
 
 int kiblnd_close_peer_conns_locked(struct kib_peer *peer, int why)
Signature · 13 lines
1.8.3.1


--------------------------------------------------------------------
Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park,
17 Krylatskaya Str., Bldg 4, Moscow 121614,
Russian Federation

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#3 lore raw
In reply to Greg Kroah-Hartman
Quoted 27 lines · Greg Kroah-Hartman
Quoted 1 line · Dmitry

In the original commit 4d99b2581effe115376402e710fbcb1c3c073769

Please use the documented way to write this:
4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd")

Quoted 1 line

was missed one hunk. Added it now to avoid issue with use after free.

And I do not understand this commit message at all.

Quoted 13 lines

---
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +0 -0
index 2ebc484..a15a625 100644
@@ -890,7 +890,8 @@ void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
 		atomic_dec(&net->ibn_nconns);
 	}
 
Quoted 5 lines

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn) · line 894

Quoted 3 lines
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +2 -1
-	kfree(conn);
+	if (free_conn)
+		kfree(conn);

This looks really odd, don't you think?

I'm not sure what the objection is here? There is an argument to this this function named "free_conn" which determines if the structure should be freed, or if the network connection is just being torn down and reconnected.

Cheers, Andreas
--
Andreas Dilger
Lustre Principal Architect
Intel Corporation

#4 lore raw
Quoted 33 lines · Andreas
Quoted 27 lines · Greg Kroah-Hartman
Quoted 1 line · Dmitry

In the original commit 4d99b2581effe115376402e710fbcb1c3c073769

Please use the documented way to write this:
4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd")

Quoted 1 line

was missed one hunk. Added it now to avoid issue with use after free.

And I do not understand this commit message at all.

Quoted 13 lines

---
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +0 -0
index 2ebc484..a15a625 100644
@@ -890,7 +890,8 @@ void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
 		atomic_dec(&net->ibn_nconns);
 	}
 
Quoted 5 lines

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn) · line 894

Quoted 3 lines
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +2 -1
-	kfree(conn);
+	if (free_conn)
+		kfree(conn);

This looks really odd, don't you think?

Quoted 4 lines

I'm not sure what the objection is here? There is an argument to this this function named "free_conn" which determines if the structure should be freed, or if the network connection is just being torn down and reconnected.

At first glance it really looks like the normal pattern of:

	if (foo_ptr)
		kfree(foo_ptr);

right?

If you don't want to free the variable, set it to NULL.

Even then, this is a horrible function, you should have 2 different ones:

	kiblnd_destroy_conn(...)
	kiblnd_free_conn()

and then just free the variable in the free_conn() function if you were going to set the free_conn variable, right?

That way no odd code paths are taken, and it's obvious what you are doing just by reading the code at the callsite.

thanks,

greg k-h

#6 lore raw
In reply to Eremin, Dmitry
Quoted 37 lines · Dmitry

The logic of the original commit 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd") was assumed conditional free of struct kib_conn if the second argument free_conn in function kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn) is true. But this hunk of code was dropped from original commit. As result the logic works wrong and current code use struct kib_conn after free.

Quoted 17 lines

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c
3317 kiblnd_destroy_conn(conn, !peer);
^^^^ Freed always (but should be conditionally)
3318
3319 spin_lock_irqsave(lock, flags);
3320 if (!peer)
3321 continue;
3322

  3323                          conn->ibc_peer = peer;
                                    ^^^^^^^^^^^^^ Use after free
  3324                          if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
  3325                                  list_add_tail(&conn->ibc_list,
                                                                       ^^^^^^^^^^^^
  3326                                                &kiblnd_data.kib_reconn_list);
  3327                          else
  3328                                  list_add_tail(&conn->ibc_list,
                                                                        ^^^^^^^^^^^^
Quoted 1 line

3329 &kiblnd_data.kib_reconn_wait);

After attached patch this code will use struct kib_conn only when it was not freed.

---
 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +0 -0
index 2ebc484..a15a625 100644

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn) · line 897

Quoted 9 lines
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +2 -1
@@ -890,7 +890,8 @@ void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
 		atomic_dec(&net->ibn_nconns);
 	}
 
-	kfree(conn);
+	if (free_conn)
+		kfree(conn);
 }
 
 int kiblnd_close_peer_conns_locked(struct kib_peer *peer, int why)

This patch needs a real "Fixes:" tag, right?

Also, as this was from 4.6, it should go to the stable trees, right? Can you resend this with that info, and then send a follow-on patch to fix this up the way I recommended so that no one is confused in the future?

thanks,

greg k-h

#7 lore raw

PATCH v2 1/1 staging: lustre: Fix avoid intensive reconnecting for ko2iblnd patch

From: Dmitry Eremin <[email protected]>

The logic of the original commit 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd") was assumed conditional free of struct kib_conn if the second argument free_conn
in function kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn) is true.
But this hunk of code was dropped from original commit. As result the logic
works wrong and current code use struct kib_conn after free.

Quoted 17 lines

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c
3317 kiblnd_destroy_conn(conn, !peer);
^^^^ Freed always (but should be conditionally)
3318
3319 spin_lock_irqsave(lock, flags);
3320 if (!peer)
3321 continue;
3322

3323  conn->ibc_peer = peer;
      ^^^^^^^^^^^^^^ Use after free
3324  if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3325      list_add_tail(&conn->ibc_list,
                         ^^^^^^^^^^^^^^ Use after free
3326                    &kiblnd_data.kib_reconn_list);
3327  else
3328      list_add_tail(&conn->ibc_list,
                         ^^^^^^^^^^^^^^ Use after free
Quoted 1 line

3329 &kiblnd_data.kib_reconn_wait);

After attached patch this code will use struct kib_conn only when it was not freed.

Cc <[email protected]> # v4.6
Fixes 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd")
---
 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +2 -1
index 2ebc484385b3..a15a625ee9b6 100644
@@ -890,7 +890,8 @@ void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
 		atomic_dec(&net->ibn_nconns);
 	}
 
-	kfree(conn);
+	if (free_conn)
+		kfree(conn);
 }
 
 int kiblnd_close_peer_conns_locked(struct kib_peer *peer, int why)
Signature · 13 lines
1.8.3.1


--------------------------------------------------------------------
Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park,
17 Krylatskaya Str., Bldg 4, Moscow 121614,
Russian Federation

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#8 lore raw

PATCH v3 1/1 staging: lustre: separate a connection destroy from free struct kib_conn

In reply to Greg Kroah-Hartman

Rewrite the logic of the original commit 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd") and move the freeing a struct kib_conn outside of the function kiblnd_destroy_conn(). The freeing of struct kib_conn should be depending on the second argument free_conn of function kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn). If it true the structure should be freed.

Fixes 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd")
---
 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c    | 7 +++----
 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.h    | 2 +-
 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c | 6 ++++--
 3 files changed, 8 insertions(+), 7 deletions(-)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +3 -4
index 2ebc484385b3..ec84edfda271 100644
@@ -824,14 +824,15 @@ struct kib_conn *kiblnd_create_conn(struct kib_peer *peer, struct rdma_cm_id *cm
 	return conn;
 
  failed_2:
-	kiblnd_destroy_conn(conn, true);
+	kiblnd_destroy_conn(conn);
+	kfree(conn);
  failed_1:
 	kfree(init_qp_attr);
  failed_0:
 	return NULL;
 }
 
-void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
+void kiblnd_destroy_conn(struct kib_conn *conn)
 {
 	struct rdma_cm_id *cmid = conn->ibc_cmid;
 	struct kib_peer *peer = conn->ibc_peer;
@@ -889,8 +890,6 @@ void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
 		rdma_destroy_id(cmid);
 		atomic_dec(&net->ibn_nconns);
 	}
-
-	kfree(conn);
 }
 
 int kiblnd_close_peer_conns_locked(struct kib_peer *peer, int why)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.h +1 -1
index 171eced213f8..b18911d09e9a 100644
@@ -1016,7 +1016,7 @@ int  kiblnd_close_stale_conns_locked(struct kib_peer *peer,
 struct kib_conn *kiblnd_create_conn(struct kib_peer *peer,
 				    struct rdma_cm_id *cmid,
 				    int state, int version);
-void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn);
+void kiblnd_destroy_conn(struct kib_conn *conn);
 void kiblnd_close_conn(struct kib_conn *conn, int error);
 void kiblnd_close_conn_locked(struct kib_conn *conn, int error);
 
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c +4 -2
index 9b3328c5d1e7..b3e7f28eb978 100644
@@ -3314,11 +3314,13 @@ static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
 			spin_unlock_irqrestore(lock, flags);
 			dropped_lock = 1;
 
-			kiblnd_destroy_conn(conn, !peer);
+			kiblnd_destroy_conn(conn);
 
 			spin_lock_irqsave(lock, flags);
-			if (!peer)
+			if (!peer) {
+				kfree(conn);
 				continue;
+			}
 
 			conn->ibc_peer = peer;
 			if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
Signature · 18 lines
1.8.3.1


--------------------------------------------------------------------
Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park,
17 Krylatskaya Str., Bldg 4, Moscow 121614,
Russian Federation

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

_______________________________________________
devel mailing list
[email protected]
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel
#9 lore raw

Re: [PATCH v3] staging: lustre: separate a connection destroy from free struct kib_conn

Quoted 7 lines · Dmitry Eremin

Rewrite the logic of the original commit 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd") and move the freeing a struct kib_conn outside of the function kiblnd_destroy_conn(). The freeing of struct kib_conn should be depending on the second argument free_conn of function kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn). If it true the structure should be freed.

Quoted 7 lines
Fixes 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd")
---
 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c    | 7 +++----
 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.h    | 2 +-
 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c | 6 ++++--
 3 files changed, 8 insertions(+), 7 deletions(-)

What changed from version 2 and 1? Always included that below the --- line please.

4th try is a charm? :)

thanks,

greg k-h
_______________________________________________
devel mailing list
[email protected]
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

#10 lore raw

PATCH v4 1/1 staging: lustre: separate a connection destroy from free struct kib_conn

The logic of the original commit 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd") was assumed conditional free of struct kib_conn if the second argument free_conn in function kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn) is true. But this hunk of code was dropped from original commit. As result the logic works wrong and current code use struct kib_conn after free.

Quoted 17 lines

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c
3317 kiblnd_destroy_conn(conn, !peer);
^^^^ Freed always (but should be conditionally)
3318
3319 spin_lock_irqsave(lock, flags);
3320 if (!peer)
3321 continue;
3322

3323  conn->ibc_peer = peer;
      ^^^^^^^^^^^^^^ Use after free
3324  if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3325      list_add_tail(&conn->ibc_list,
                         ^^^^^^^^^^^^^^ Use after free
3326                    &kiblnd_data.kib_reconn_list);
3327  else
3328      list_add_tail(&conn->ibc_list,
                         ^^^^^^^^^^^^^^ Use after free
Quoted 1 line

3329 &kiblnd_data.kib_reconn_wait);

To avoid confusion this fix moved the freeing a struct kib_conn outside of the function kiblnd_destroy_conn() and free as it was intended in original commit.

Cc <[email protected]> # v4.6
Fixes 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd")

---
Changes in v4:
- fixed the issue with use after free by moving the freeing a struct kib_conn outside of the function kiblnd_destroy_conn()

 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c    | 7 +++----
 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.h    | 2 +-
 drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c | 6 ++++--
 3 files changed, 8 insertions(+), 7 deletions(-)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +3 -4
index 2ebc484385b3..ec84edfda271 100644
@@ -824,14 +824,15 @@ struct kib_conn *kiblnd_create_conn(struct kib_peer *peer, struct rdma_cm_id *cm
 	return conn;
 
  failed_2:
-	kiblnd_destroy_conn(conn, true);
+	kiblnd_destroy_conn(conn);
+	kfree(conn);
  failed_1:
 	kfree(init_qp_attr);
  failed_0:
 	return NULL;
 }
 
-void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
+void kiblnd_destroy_conn(struct kib_conn *conn)
 {
 	struct rdma_cm_id *cmid = conn->ibc_cmid;
 	struct kib_peer *peer = conn->ibc_peer;
@@ -889,8 +890,6 @@ void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
 		rdma_destroy_id(cmid);
 		atomic_dec(&net->ibn_nconns);
 	}
-
-	kfree(conn);
 }
 
 int kiblnd_close_peer_conns_locked(struct kib_peer *peer, int why)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.h +1 -1
index 171eced213f8..b18911d09e9a 100644
@@ -1016,7 +1016,7 @@ int  kiblnd_close_stale_conns_locked(struct kib_peer *peer,
 struct kib_conn *kiblnd_create_conn(struct kib_peer *peer,
 				    struct rdma_cm_id *cmid,
 				    int state, int version);
-void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn);
+void kiblnd_destroy_conn(struct kib_conn *conn);
 void kiblnd_close_conn(struct kib_conn *conn, int error);
 void kiblnd_close_conn_locked(struct kib_conn *conn, int error);
 
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c +4 -2
index 9b3328c5d1e7..b3e7f28eb978 100644
@@ -3314,11 +3314,13 @@ static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
 			spin_unlock_irqrestore(lock, flags);
 			dropped_lock = 1;
 
-			kiblnd_destroy_conn(conn, !peer);
+			kiblnd_destroy_conn(conn);
 
 			spin_lock_irqsave(lock, flags);
-			if (!peer)
+			if (!peer) {
+				kfree(conn);
 				continue;
+			}
 
 			conn->ibc_peer = peer;
 			if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
Signature · 13 lines
1.8.3.1


--------------------------------------------------------------------
Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park,
17 Krylatskaya Str., Bldg 4, Moscow 121614,
Russian Federation

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
Reviewed-by on v4 1/1
#11 lore raw

Re: [PATCH v4] staging: lustre: separate a connection destroy from free struct kib_conn

Quoted 27 lines · Dmitry

The logic of the original commit 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd") was assumed conditional free of struct kib_conn if the second argument free_conn in function kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn) is true. But this hunk of code was dropped from original commit. As result the logic works wrong and current code use struct kib_conn after free.

Quoted 17 lines

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c
3317 kiblnd_destroy_conn(conn, !peer);
^^^^ Freed always (but should be conditionally)
3318
3319 spin_lock_irqsave(lock, flags);
3320 if (!peer)
3321 continue;
3322

3323  conn->ibc_peer = peer;
     ^^^^^^^^^^^^^^ Use after free
3324  if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3325      list_add_tail(&conn->ibc_list,
                        ^^^^^^^^^^^^^^ Use after free
3326                    &kiblnd_data.kib_reconn_list);
3327  else
3328      list_add_tail(&conn->ibc_list,
                        ^^^^^^^^^^^^^^ Use after free
Quoted 1 line

3329 &kiblnd_data.kib_reconn_wait);

Quoted 7 lines

To avoid confusion this fix moved the freeing a struct kib_conn outside of the function kiblnd_destroy_conn() and free as it was intended in original commit.

Cc <[email protected]> # v4.6
Fixes 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd")
Quoted 77 lines

---
Changes in v4:
- fixed the issue with use after free by moving the freeing a struct kib_conn outside of the function kiblnd_destroy_conn()

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c    | 7 +++----
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.h    | 2 +-
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c | 6 ++++--
3 files changed, 8 insertions(+), 7 deletions(-)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +3 -4
index 2ebc484385b3..ec84edfda271 100644
@@ -824,14 +824,15 @@ struct kib_conn *kiblnd_create_conn(struct kib_peer *peer, struct rdma_cm_id *cm
 	return conn;
 
 failed_2:
-	kiblnd_destroy_conn(conn, true);
+	kiblnd_destroy_conn(conn);
+	kfree(conn);
 failed_1:
 	kfree(init_qp_attr);
 failed_0:
 	return NULL;
 }
 
-void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
+void kiblnd_destroy_conn(struct kib_conn *conn)
 {
 	struct rdma_cm_id *cmid = conn->ibc_cmid;
 	struct kib_peer *peer = conn->ibc_peer;
@@ -889,8 +890,6 @@ void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
 		rdma_destroy_id(cmid);
 		atomic_dec(&net->ibn_nconns);
 	}
-
-	kfree(conn);
 }
 
 int kiblnd_close_peer_conns_locked(struct kib_peer *peer, int why)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.h +1 -1
index 171eced213f8..b18911d09e9a 100644
@@ -1016,7 +1016,7 @@ int  kiblnd_close_stale_conns_locked(struct kib_peer *peer,
 struct kib_conn *kiblnd_create_conn(struct kib_peer *peer,
 				    struct rdma_cm_id *cmid,
 				    int state, int version);
-void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn);
+void kiblnd_destroy_conn(struct kib_conn *conn);
 void kiblnd_close_conn(struct kib_conn *conn, int error);
 void kiblnd_close_conn_locked(struct kib_conn *conn, int error);
 
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c +4 -2
index 9b3328c5d1e7..b3e7f28eb978 100644
@@ -3314,11 +3314,13 @@ static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
 			spin_unlock_irqrestore(lock, flags);
 			dropped_lock = 1;
 
-			kiblnd_destroy_conn(conn, !peer);
+			kiblnd_destroy_conn(conn);
 
 			spin_lock_irqsave(lock, flags);
-			if (!peer)
+			if (!peer) {
+				kfree(conn);
 				continue;
+			}
 
 			conn->ibc_peer = peer;
 			if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
Signature · 1 line
1.8.3.1

Cheers, Andreas
--
Andreas Dilger
Lustre Principal Architect
Intel Corporation