Re: [PATCH v5 1/2] ceph: Use unsigned types for snapshot split counts

From: Xiubo Li

Date: Wed Dec 02 2026 - 19:23:23 EST


Hi Kaho,

On Tue, Dec 01, 2026 at 11:50:40PM -0500, Kaho Jiang wrote:
> 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.

I tested this on ceph-client/testing (3f90412e989c) with a KASAN kernel
and a test-only module param that overrides num_split_inos in
ceph_handle_snap() before the decode.

Without this patch, with num_split_inos = 0xfffffff0 on an UPDATE:

BUG: KASAN: slab-use-after-free in ceph_update_snap_trace+0xdf0/0x12a0 [ceph]
ceph_update_snap_trace
ceph_handle_snap
mds_dispatch
ceph_con_process_message

On a SPLIT the same value makes the client decode the realm info from
memory before the message buffer. KASAN did not catch that one because
it landed in a live object.

With the patch, both UPDATE and SPLIT are rejected with "corrupt snap
message from mds0" and no KASAN report.

> @@ -1076,8 +1076,10 @@ void ceph_handle_snap(struct ceph_mds_client *mdsc,
> split_inos = p;
> + ceph_decode_need(&p, e, size_mul(num_split_inos, sizeof(u64)), bad);
> p += sizeof(u64) * num_split_inos;
> split_realms = p;
> + ceph_decode_need(&p, e, size_mul(num_split_realms, sizeof(u64)), bad);

One thing to consider, since this patch is tagged for stable and 2/2 is
not: "bad" drops the SPLIT and leaves the mount writable. The inodes
that should have moved to the new realm stay in the old one and keep
writing with the old snap context, so the snapshot gets overwritten.

I checked it: hold a file open in a directory, create .snap/s1 with the
SPLIT dropped by this path, then append to the file and read it back
from a second mount:

f2: x y
.snap/s1/f2: x y <- should be "x"

With 2/2 applied the client is fenced instead. The append fails with
ESTALE, and the snapshot still reads "x".

Other malformed messages already went to "bad", so this patch does not
make anything worse. Still, a stable kernel that takes only this patch
gets the drop. Could you either fence here as well, or tag 2/2 for
stable too?

With that addressed:

Tested-by: Xiubo Li <xiubo.li@xxxxxxxxx>

Thanks,
Xiubo

On Wed, 2 Dec 2026 at 05:51, Kaho Jiang <kj2864@xxxxxxx> wrote:
>
> 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@xxxxxxxxxxxxxxx
> Reported-by: co+408fcc361565713f@xxxxxxx
> Assisted-by: LLM
> Signed-off-by: Kaho Jiang <kj2864@xxxxxxx>
> ---
> fs/ceph/snap.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/fs/ceph/snap.c b/fs/ceph/snap.c
> index 54c022d1fd68..3a1b3beaacf5 100644
> --- a/fs/ceph/snap.c
> +++ b/fs/ceph/snap.c
> @@ -1039,7 +1039,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;
> @@ -1076,8 +1076,10 @@ 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)), bad);
> p += sizeof(u64) * num_split_inos;
> split_realms = p;
> + ceph_decode_need(&p, e, size_mul(num_split_realms, sizeof(u64)), bad);
> p += sizeof(u64) * num_split_realms;
> ceph_decode_need(&p, e, sizeof(*ri), bad);
> /* we will peek at realm info here, but will _not_
> @@ -1156,8 +1158,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)), bad);
> + ceph_decode_skip_n(&p, e, size_mul(num_split_realms, sizeof(u64)), bad);
> }
>
> /*
> --
> 2.43.0
>