Skip to contentSkip to conversation

ceph: fix potential mdsc use-after-free crash

PATCH In review [email protected] 2 participants 5 messages Jul 1, 2020 Ceph client

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 ceph: fix potential mdsc use-after-free crash

From: Xiubo Li <[email protected]>

Make sure the delayed work stopped before releasing the resources.

Because the cancel_delayed_work_sync() will only guarantee that the work finishes executing if the work is already in the ->worklist. That means after the cancel_delayed_work_sync() returns and in case if the work will re-arm itself, it will leave the work requeued. And if we release the resources before the delayed work to run again we will hit the use-after-free bug.

URL: https://tracker.ceph.com/issues/46293

---
 fs/ceph/mds_client.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
fs/ceph/mds_client.c +13 -1
index d5e523c..9a09d12 100644
@@ -4330,6 +4330,9 @@ static void delayed_work(struct work_struct *work)
 
 	dout("mdsc delayed_work\n");
 
+	if (mdsc->stopping)
+		return;
+
 	mutex_lock(&mdsc->mutex);
 	renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
 	renew_caps = time_after_eq(jiffies, HZ*renew_interval +
@@ -4689,7 +4692,16 @@ void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
 {
 	dout("stop\n");
-	cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
+	/*
+	 * Make sure the delayed work stopped before releasing
+	 * the resources.
+	 *
+	 * Because the cancel_delayed_work_sync() will only
+	 * guarantee that the work finishes executing. But the
+	 * delayed work will re-arm itself again after that.
+	 */
+	flush_delayed_work(&mdsc->delayed_work);
+
 	if (mdsc->mdsmap)
 		ceph_mdsmap_destroy(mdsc->mdsmap);
 	kfree(mdsc->sessions);
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 39 lines · On Wed, 2020-07-01 at 01:52 -0400, [email protected] wrote:

From: Xiubo Li <[email protected]>

Make sure the delayed work stopped before releasing the resources.

Because the cancel_delayed_work_sync() will only guarantee that the work finishes executing if the work is already in the ->worklist. That means after the cancel_delayed_work_sync() returns and in case if the work will re-arm itself, it will leave the work requeued. And if we release the resources before the delayed work to run again we will hit the use-after-free bug.

URL: https://tracker.ceph.com/issues/46293

---
 fs/ceph/mds_client.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
fs/ceph/mds_client.c +6 -1
index d5e523c..9a09d12 100644
@@ -4330,6 +4330,9 @@ static void delayed_work(struct work_struct *work)
 
 	dout("mdsc delayed_work\n");
 
+	if (mdsc->stopping)
+		return;
+
 	mutex_lock(&mdsc->mutex);
 	renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
 	renew_caps = time_after_eq(jiffies, HZ*renew_interval +
@@ -4689,7 +4692,16 @@ void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
 {
 	dout("stop\n");
-	cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
+	/*
+	 * Make sure the delayed work stopped before releasing
+	 * the resources.

fs/ceph/mds_client.c void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc) · line 4707

Quoted 10 lines
fs/ceph/mds_client.c +7 -0
+	 *
+	 * Because the cancel_delayed_work_sync() will only
+	 * guarantee that the work finishes executing. But the
+	 * delayed work will re-arm itself again after that.
+	 */
+	flush_delayed_work(&mdsc->delayed_work);
+
 	if (mdsc->mdsmap)
 		ceph_mdsmap_destroy(mdsc->mdsmap);
 	kfree(mdsc->sessions);

This patch looks fine, but the subject says [PATCH 1/3]. Were there others in this series that didn't make it to the list for some reason?

Thanks,

Signature · 1 line
Jeff Layton <[email protected]>
author
#2 lore raw
Quoted 44 lines · Jeff Layton
Quoted 43 lines · On Wed, 2020-07-01 at 01:52 -0400, [email protected] wrote:

From: Xiubo Li <[email protected]>

Make sure the delayed work stopped before releasing the resources.

Because the cancel_delayed_work_sync() will only guarantee that the work finishes executing if the work is already in the ->worklist. That means after the cancel_delayed_work_sync() returns and in case if the work will re-arm itself, it will leave the work requeued. And if we release the resources before the delayed work to run again we will hit the use-after-free bug.

URL: https://tracker.ceph.com/issues/46293

---
  fs/ceph/mds_client.c | 14 +++++++++++++-
  1 file changed, 13 insertions(+), 1 deletion(-)
fs/ceph/mds_client.c +10 -1
index d5e523c..9a09d12 100644
@@ -4330,6 +4330,9 @@ static void delayed_work(struct work_struct *work)
 
  	dout("mdsc delayed_work\n");
 
+	if (mdsc->stopping)
+		return;
+
  	mutex_lock(&mdsc->mutex);
  	renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
  	renew_caps = time_after_eq(jiffies, HZ*renew_interval +
@@ -4689,7 +4692,16 @@ void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
  static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
  {
  	dout("stop\n");
-	cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
+	/*
+	 * Make sure the delayed work stopped before releasing
+	 * the resources.
+	 *
+	 * Because the cancel_delayed_work_sync() will only
+	 * guarantee that the work finishes executing. But the
+	 * delayed work will re-arm itself again after that.
Quoted 8 lines

fs/ceph/mds_client.c void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc) · line 4707

Quoted 6 lines
fs/ceph/mds_client.c +3 -0
+	 */
+	flush_delayed_work(&mdsc->delayed_work);
+
  	if (mdsc->mdsmap)
  		ceph_mdsmap_destroy(mdsc->mdsmap);
  	kfree(mdsc->sessions);

This patch looks fine, but the subject says [PATCH 1/3]. Were there others in this series that didn't make it to the list for some reason?

Sorry for confusing.

Just generated this patch with the metrics series and forget to fix it before sending out.

BRs

Xiubo

Quoted 1 line

Thanks,

#3 lore raw
Quoted 54 lines · Xiubo Li
Quoted 44 lines · Jeff Layton
Quoted 43 lines · On Wed, 2020-07-01 at 01:52 -0400, [email protected] wrote:

From: Xiubo Li <[email protected]>

Make sure the delayed work stopped before releasing the resources.

Because the cancel_delayed_work_sync() will only guarantee that the work finishes executing if the work is already in the ->worklist. That means after the cancel_delayed_work_sync() returns and in case if the work will re-arm itself, it will leave the work requeued. And if we release the resources before the delayed work to run again we will hit the use-after-free bug.

URL: https://tracker.ceph.com/issues/46293

---
  fs/ceph/mds_client.c | 14 +++++++++++++-
  1 file changed, 13 insertions(+), 1 deletion(-)
fs/ceph/mds_client.c +10 -1
index d5e523c..9a09d12 100644
@@ -4330,6 +4330,9 @@ static void delayed_work(struct work_struct *work)
 
  	dout("mdsc delayed_work\n");
 
+	if (mdsc->stopping)
+		return;
+
  	mutex_lock(&mdsc->mutex);
  	renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
  	renew_caps = time_after_eq(jiffies, HZ*renew_interval +
@@ -4689,7 +4692,16 @@ void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
  static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
  {
  	dout("stop\n");
-	cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
+	/*
+	 * Make sure the delayed work stopped before releasing
+	 * the resources.
+	 *
+	 * Because the cancel_delayed_work_sync() will only
+	 * guarantee that the work finishes executing. But the
+	 * delayed work will re-arm itself again after that.
Quoted 8 lines

fs/ceph/mds_client.c void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc) · line 4707

Quoted 6 lines
fs/ceph/mds_client.c +3 -0
+	 */
+	flush_delayed_work(&mdsc->delayed_work);
+
  	if (mdsc->mdsmap)
  		ceph_mdsmap_destroy(mdsc->mdsmap);
  	kfree(mdsc->sessions);

This patch looks fine, but the subject says [PATCH 1/3]. Were there others in this series that didn't make it to the list for some reason?

Quoted 6 lines

Sorry for confusing.

Just generated this patch with the metrics series and forget to fix it before sending out.

No worries. Just making sure before I merged it. Merged patch into testing branch.

Thanks!

Signature · 1 line
Jeff Layton <[email protected]>
author
#4 lore raw
Quoted 54 lines · Jeff Layton
Quoted 53 lines · Xiubo Li
Quoted 52 lines · Jeff Layton
Quoted 39 lines · On Wed, 2020-07-01 at 01:52 -0400, [email protected] wrote:

From: Xiubo Li <[email protected]>

Make sure the delayed work stopped before releasing the resources.

Because the cancel_delayed_work_sync() will only guarantee that the work finishes executing if the work is already in the ->worklist. That means after the cancel_delayed_work_sync() returns and in case if the work will re-arm itself, it will leave the work requeued. And if we release the resources before the delayed work to run again we will hit the use-after-free bug.

URL: https://tracker.ceph.com/issues/46293

---
   fs/ceph/mds_client.c | 14 +++++++++++++-
   1 file changed, 13 insertions(+), 1 deletion(-)
fs/ceph/mds_client.c +6 -1
index d5e523c..9a09d12 100644
@@ -4330,6 +4330,9 @@ static void delayed_work(struct work_struct *work)
 
   	dout("mdsc delayed_work\n");
 
+	if (mdsc->stopping)
+		return;
+
   	mutex_lock(&mdsc->mutex);
   	renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
   	renew_caps = time_after_eq(jiffies, HZ*renew_interval +
@@ -4689,7 +4692,16 @@ void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
   static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
   {
   	dout("stop\n");
-	cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
+	/*
+	 * Make sure the delayed work stopped before releasing
+	 * the resources.

fs/ceph/mds_client.c void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc) · line 4707

Quoted 10 lines
fs/ceph/mds_client.c +7 -0
+	 *
+	 * Because the cancel_delayed_work_sync() will only
+	 * guarantee that the work finishes executing. But the
+	 * delayed work will re-arm itself again after that.
+	 */
+	flush_delayed_work(&mdsc->delayed_work);
+
   	if (mdsc->mdsmap)
   		ceph_mdsmap_destroy(mdsc->mdsmap);
   	kfree(mdsc->sessions);

This patch looks fine, but the subject says [PATCH 1/3]. Were there others in this series that didn't make it to the list for some reason?

Quoted 8 lines
Quoted 6 lines

Sorry for confusing.

Just generated this patch with the metrics series and forget to fix it before sending out.

No worries. Just making sure before I merged it. Merged patch into testing branch.

Thanks Jeff.

BRs

Quoted 2 lines

Thanks!