mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4] ceph: Use unsigned types for snapshot split counts
@ 2026-11-26  3:27 Kaho Jiang
  2026-11-27  0:34 ` Xiubo Li
  2026-11-30 15:44 ` Alex Markuze
  0 siblings, 2 replies; 3+ messages in thread
From: Kaho Jiang @ 2026-11-26  3:27 UTC (permalink / raw)
  To: Ilya Dryomov, Alex Markuze, Xiubo Li, Viacheslav Dubeyko
  Cc: ceph-devel, linux-kernel, stable, co+408fcc361565713f, Kaho Jiang

ceph_handle_snap() stores the unsigned 32-bit split counts from the
message header in signed integers. Values with the high bit set become
negative and, on 64-bit systems, cause the subsequent pointer arithmetic
to move backwards before the message buffer.

BUG: KASAN: slab-out-of-bounds in ceph_update_snap_trace (fs/ceph/snap.c:815)
Read of size 8 at addr ffff888024f45478 by task kworker/1:3/2220
CPU: 1 UID: 0 PID: 2220 Comm: kworker/1:3 Not tainted 7.4.0-rc2-g7475ea8b5861 #1 PREEMPT(full)
Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
Workqueue: ceph-msgr ceph_con_workfn
Call Trace:
 <TASK>
 dump_stack_lvl (lib/dump_stack.c:94 lib/dump_stack.c:120)
 print_report (mm/kasan/report.c:378 mm/kasan/report.c:482)
 ceph_update_snap_trace (fs/ceph/snap.c:815)
 ceph_handle_snap (fs/ceph/snap.c:1057)
 __pfx_ceph_update_snap_trace (./include/linux/list.h:172)
 down_write (kernel/locking/rwsem.c:1350 kernel/locking/rwsem.c:1356 kernel/locking/rwsem.c:1632)
 __pfx_down_write (kernel/locking/rwsem.c:602)
 ceph_handle_snap (fs/ceph/snap.c:1158)
 mds_dispatch (fs/ceph/mds_client.c:7298)
 find_held_lock (kernel/locking/lockdep.c:5401)
 ceph_con_process_message (net/ceph/messenger.c:1413)
 ceph_con_process_message (net/ceph/messenger.c:1424)
 ceph_con_in_msg_alloc (net/ceph/messenger.c:2094)
 ceph_con_v1_try_read (net/ceph/messenger_v1.c:1434)
 ceph_con_workfn (net/ceph/messenger.c:1576)
 process_one_work (kernel/workqueue.c:3495)
 worker_thread (kernel/workqueue.c:3578 kernel/workqueue.c:3659)
 kthread (kernel/kthread.c:436)
 ret_from_fork (arch/x86/kernel/process.c:158)
 ret_from_fork_asm (arch/x86/entry/entry_64.S:245)
 </TASK>
Kernel Offset: disabled

Use u32 for both counts and validate both split arrays against the
remaining message buffer before advancing the decode pointer. Use
size_mul() to detect multiplication overflow on 32-bit systems, and
ceph_decode_skip_n() for checked skips in the non-split branch.

Fixes: 963b61eb041e ("ceph: snapshot management")
Cc: stable@vger.kernel.org
Reported-by: co+408fcc361565713f@bugs.sh
Assisted-by: LLM
Signed-off-by: Kaho Jiang <kj2864@nyu.edu>
---
 fs/ceph/mds_client.c | 34 +++++++++++++++++++++++++++++++++-
 fs/ceph/mds_client.h |  3 +++
 fs/ceph/snap.c       | 33 +++++++++++----------------------
 3 files changed, 47 insertions(+), 23 deletions(-)

diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c
index 15df9d22008d..4b416fa2ad1a 100644
--- a/fs/ceph/mds_client.c
+++ b/fs/ceph/mds_client.c
@@ -69,6 +69,7 @@ static void __wake_requests(struct ceph_mds_client *mdsc,
 static void ceph_cap_release_work(struct work_struct *work);
 static void ceph_cap_reclaim_work(struct work_struct *work);
 static void ceph_mdsc_reset_workfn(struct work_struct *work);
+static void ceph_mdsc_snap_fence_workfn(struct work_struct *work);
 
 static const struct ceph_connection_operations mds_con_ops;
 
@@ -4435,7 +4436,7 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
 
 	/* Defer closing the sessions after s_mutex lock being released */
 	if (close_sessions)
-		ceph_mdsc_close_sessions(mdsc);
+		ceph_mdsc_schedule_snap_fence(mdsc);
 	ceph_blog_exit(&__ji);
 	return;
 }
@@ -6568,6 +6569,8 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc)
 	init_waitqueue_head(&mdsc->reset_state.blocked_wq);
 	atomic_set(&mdsc->reset_state.blocked_requests, 0);
 	INIT_WORK(&mdsc->reset_work, ceph_mdsc_reset_workfn);
+	INIT_WORK(&mdsc->snap_fence_work, ceph_mdsc_snap_fence_workfn);
+	atomic_set(&mdsc->snap_fence_pending, 0);
 
 	ceph_caps_init(mdsc);
 	ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
@@ -6962,6 +6965,34 @@ static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
 	return atomic_read(&mdsc->num_sessions) <= skipped;
 }
 
+static void ceph_mdsc_snap_fence_workfn(struct work_struct *work)
+{
+	struct ceph_mds_client *mdsc =
+		container_of(work, struct ceph_mds_client, snap_fence_work);
+	struct ceph_client *cl = mdsc->fsc->client;
+	int ret;
+
+	ret = ceph_monc_blocklist_add(&cl->monc, &cl->msgr.inst.addr);
+	if (ret)
+		pr_err_client(cl, "failed to blocklist %s: %d\n",
+			      ceph_pr_addr(&cl->msgr.inst.addr), ret);
+
+	ceph_mdsc_close_sessions(mdsc);
+	ceph_dec_osd_stopping_blocker(mdsc);
+}
+
+void ceph_mdsc_schedule_snap_fence(struct ceph_mds_client *mdsc)
+{
+	if (atomic_cmpxchg(&mdsc->snap_fence_pending, 0, 1))
+		return;
+
+	if (!ceph_inc_osd_stopping_blocker(mdsc))
+		return;
+
+	if (!queue_work(system_dfl_wq, &mdsc->snap_fence_work))
+		ceph_dec_osd_stopping_blocker(mdsc);
+}
+
 /*
  * called after sb is ro or when metadata corrupted.
  */
@@ -7120,6 +7151,7 @@ void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
 	wake_up_all(&mdsc->reset_state.blocked_wq);
 
 	cancel_work_sync(&mdsc->reset_work);
+	cancel_work_sync(&mdsc->snap_fence_work);
 	ceph_mdsc_stop(mdsc);
 
 	ceph_metric_destroy(&mdsc->metric);
diff --git a/fs/ceph/mds_client.h b/fs/ceph/mds_client.h
index ff9ba09f3b59..65d0fd878741 100644
--- a/fs/ceph/mds_client.h
+++ b/fs/ceph/mds_client.h
@@ -590,6 +590,8 @@ struct ceph_mds_client {
 	struct ceph_client_metric metric;
 	struct work_struct	reset_work;
 	struct ceph_client_reset_state reset_state;
+	struct work_struct	snap_fence_work;
+	atomic_t		snap_fence_pending;
 	struct ceph_subvolume_metrics_tracker subvol_metrics;
 
 	/* Subvolume metrics send tracking */
@@ -633,6 +635,7 @@ int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc);
 
 extern int ceph_mdsc_init(struct ceph_fs_client *fsc);
 extern void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc);
+extern void ceph_mdsc_schedule_snap_fence(struct ceph_mds_client *mdsc);
 extern void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc);
 extern void ceph_mdsc_destroy(struct ceph_fs_client *fsc);
 
diff --git a/fs/ceph/snap.c b/fs/ceph/snap.c
index e03d89db61fe..968486b0c2d4 100644
--- a/fs/ceph/snap.c
+++ b/fs/ceph/snap.c
@@ -796,11 +796,9 @@ int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
 	struct ceph_snap_realm *realm;
 	struct ceph_snap_realm *first_realm = NULL;
 	struct ceph_snap_realm *realm_to_rebuild = NULL;
-	struct ceph_client *client = mdsc->fsc->client;
 	size_t snaps_len;
 	int rebuild_snapcs;
 	int err = -ENOMEM;
-	int ret;
 	LIST_HEAD(dirty_realms);
 
 	lockdep_assert_held_write(&mdsc->snap_rwsem);
@@ -924,22 +922,8 @@ int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
 	 * When receiving a corrupted snap trace we don't know what
 	 * exactly has happened in MDS side. And we shouldn't continue
 	 * writing to OSD, which may corrupt the snapshot contents.
-	 *
-	 * Just try to blocklist this kclient and then this kclient
-	 * must be remounted to continue after the corrupted metadata
-	 * fixed in the MDS side.
 	 */
 	WRITE_ONCE(mdsc->fsc->mount_state, CEPH_MOUNT_FENCE_IO);
-	ret = ceph_monc_blocklist_add(&client->monc, &client->msgr.inst.addr);
-	if (ret)
-		pr_err_client(cl, "failed to blocklist %s: %d\n",
-			      ceph_pr_addr(&client->msgr.inst.addr), ret);
-
-	pr_warn_ratelimited_client(cl, "%s %s%sdo remount to continue%s",
-			    __func__,
-			    ret ? "" : ceph_pr_addr(&client->msgr.inst.addr),
-			    ret ? "" : " was blocklisted, ",
-			    err == -EIO ? " after corrupted snaptrace is fixed" : "");
 
 	return err;
 }
@@ -1039,7 +1023,7 @@ void ceph_handle_snap(struct ceph_mds_client *mdsc,
 	void *p = msg->front.iov_base;
 	void *e = p + msg->front.iov_len;
 	struct ceph_mds_snap_head *h;
-	int num_split_inos, num_split_realms;
+	u32 num_split_inos, num_split_realms;
 	__le64 *split_inos = NULL, *split_realms = NULL;
 	int i;
 	int locked_rwsem = 0;
@@ -1053,7 +1037,7 @@ void ceph_handle_snap(struct ceph_mds_client *mdsc,
 
 	/* decode */
 	if (msg->front.iov_len < sizeof(*h))
-		goto bad;
+		goto fail;
 	h = p;
 	op = le32_to_cpu(h->op);
 	split = le64_to_cpu(h->split);   /* non-zero if we are splitting an
@@ -1079,10 +1063,12 @@ void ceph_handle_snap(struct ceph_mds_client *mdsc,
 		 * child.
 		 */
 		split_inos = p;
+		ceph_decode_need(&p, e, size_mul(num_split_inos, sizeof(u64)), fail);
 		p += sizeof(u64) * num_split_inos;
 		split_realms = p;
+		ceph_decode_need(&p, e, size_mul(num_split_realms, sizeof(u64)), fail);
 		p += sizeof(u64) * num_split_realms;
-		ceph_decode_need(&p, e, sizeof(*ri), bad);
+		ceph_decode_need(&p, e, sizeof(*ri), fail);
 		/* we will peek at realm info here, but will _not_
 		 * advance p, as the realm update will occur below in
 		 * ceph_update_snap_trace. */
@@ -1159,8 +1145,8 @@ void ceph_handle_snap(struct ceph_mds_client *mdsc,
 		 * positioned at the start of realm info, as expected by
 		 * ceph_update_snap_trace().
 		 */
-		p += sizeof(u64) * num_split_inos;
-		p += sizeof(u64) * num_split_realms;
+		ceph_decode_skip_n(&p, e, size_mul(num_split_inos, sizeof(u64)), fail);
+		ceph_decode_skip_n(&p, e, size_mul(num_split_realms, sizeof(u64)), fail);
 	}
 
 	/*
@@ -1187,6 +1173,9 @@ void ceph_handle_snap(struct ceph_mds_client *mdsc,
 	ceph_blog_exit(&__ji);
 	return;
 
+fail:
+	WRITE_ONCE(mdsc->fsc->mount_state, CEPH_MOUNT_FENCE_IO);
+	close_sessions = true;
 bad:
 	pr_err_client(cl, "corrupt snap message from mds%d\n", mds);
 	ceph_msg_dump(msg);
@@ -1197,7 +1186,7 @@ void ceph_handle_snap(struct ceph_mds_client *mdsc,
 	ceph_dec_mds_stopping_blocker(mdsc);
 
 	if (close_sessions)
-		ceph_mdsc_close_sessions(mdsc);
+		ceph_mdsc_schedule_snap_fence(mdsc);
 	ceph_blog_exit(&__ji);
 	return;
 }
-- 
2.43.0


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

end of thread, other threads:[~2026-11-30 15:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26  3:27 [PATCH v4] ceph: Use unsigned types for snapshot split counts Kaho Jiang
2026-11-27  0:34 ` Xiubo Li
2026-11-30 15:44 ` Alex Markuze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®