mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] libceph: don't reuse a middle buffer that is too small
@ 2026-11-16 10:35 Xingyuan Mo
  0 siblings, 0 replies; only message in thread
From: Xingyuan Mo @ 2026-11-16 10:35 UTC (permalink / raw)
  To: Ilya Dryomov, Alex Markuze, Xiubo Li, Viacheslav Dubeyko
  Cc: open list:CEPH COMMON CODE (LIBCEPH), open list, Xingyuan Mo, stable

con->ops->alloc_msg() doesn't necessarily return a brand new message.
The mon client hands out its m_auth_reply and m_subscribe_ack
singletons, osd_client reuses req->r_reply, and so on. A message like
that may still be carrying the middle allocated for the previous
message of the same type, but ceph_con_in_msg_alloc() checks only
whether ->middle is NULL:

	if (middle_len && !con->in_msg->middle) {

If the new message declares a larger middle_len, the receive path
writes past the end of the old allocation. Below is a KASAN report
from a fake monitor that answered an ms_mode=legacy mount with two
AUTH_REPLY messages, declaring middle_len of 64 bytes in the first
one and 2048 bytes in the second:

BUG: KASAN: slab-out-of-bounds in _copy_to_iter+0x81b/0x1630
Write of size 2048 at addr ffff88800df07200 by task kworker/0:1/11

CPU: 0 UID: 0 PID: 11 Comm: kworker/0:1 Not tainted 7.4.0-rc3 #24 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+0x5f/0x80
 print_report+0xd1/0x610
 ? __pfx__raw_spin_lock_irqsave+0x10/0x10
 ? kasan_complete_mode_report_info+0x2a/0x200
 kasan_report+0xe5/0x120
 ? _copy_to_iter+0x81b/0x1630
 ? _copy_to_iter+0x81b/0x1630
 kasan_check_range+0x10c/0x1c0
 __asan_memcpy+0x3f/0x70
 _copy_to_iter+0x81b/0x1630
 ? __pfx__copy_to_iter+0x10/0x10
 ? mod_timer+0xf/0x20
 ? sk_reset_timer+0x19/0x70
 ? __pfx__copy_to_iter+0x10/0x10
 simple_copy_to_iter+0x19/0x30
 __skb_datagram_iter+0x40b/0x790
 ? skb_release_data+0x4ca/0x660
 ? __pfx_simple_copy_to_iter+0x10/0x10
 skb_copy_datagram_iter+0x40/0x130
 ? tcp_cleanup_rbuf+0xfe/0x150
 tcp_recvmsg_locked+0x154c/0x29d0
 ? __pfx_tcp_recvmsg_locked+0x10/0x10
 ? _raw_spin_unlock_bh+0x40/0x60
 tcp_recvmsg+0x120/0x590
 ? __pfx_tcp_recvmsg+0x10/0x10
 ? release_sock+0x176/0x230
 ? __pfx_tcp_recvmsg+0x10/0x10
 ? __pfx_inet_recvmsg+0x10/0x10
 inet_recvmsg+0xa6/0x320
 ? __pfx_inet_recvmsg+0x10/0x10
 sock_recvmsg+0x14c/0x1a0
 ceph_tcp_recvmsg+0xca/0x110
 ? __pfx_ceph_tcp_recvmsg+0x10/0x10
 ? __pfx_inet_recvmsg+0x10/0x10
 ? mutex_lock+0x7a/0xd0
 ? __pfx_mutex_lock+0x10/0x10
 read_partial_message_chunk+0xa3/0x220
 ceph_con_v1_try_read+0x13ee/0x6400
 ? update_cfs_rq_load_avg+0x5f/0x5a0
 ? update_se+0x2f8/0x1110
 ? __pfx_ceph_con_v1_try_read+0x10/0x10
 ? __kasan_check_write+0x18/0x20
 ? mutex_lock+0x7a/0xd0
 ? __pfx_mutex_lock+0x10/0x10
 ? __pfx_pick_task_fair+0x10/0x10
 ceph_con_workfn+0x82b/0x1080
 process_one_work+0x6ea/0x1190
 ? __pfx_process_one_work+0x10/0x10
 ? __pfx_ceph_con_workfn+0x10/0x10
 ? assign_work+0x123/0x360
 worker_thread+0x45b/0xc90
 ? __pfx__raw_spin_lock_irqsave+0x10/0x10
 ? __pfx_worker_thread+0x10/0x10
 kthread+0x2d9/0x3c0
 ? __pfx_kthread+0x10/0x10
 ret_from_fork+0x380/0x5b0
 ? __pfx_ret_from_fork+0x10/0x10
 ? __kasan_check_read+0x15/0x20
 ? __switch_to+0x40/0xea0
 ? __switch_to_asm+0x39/0x70
 ? __pfx_kthread+0x10/0x10
 ret_from_fork_asm+0x1a/0x30
 </TASK>

Allocated by task 11:
 kasan_save_stack+0x3d/0x60
 kasan_save_track+0x18/0x40
 kasan_save_alloc_info+0x3b/0x50
 __kasan_kmalloc+0x9c/0xa0
 __kvmalloc_node_noprof+0x1f0/0x590
 ceph_buffer_new+0x9e/0x190
 ceph_con_in_msg_alloc+0x317/0x500
 ceph_con_v1_try_read+0x1868/0x6400
 ceph_con_workfn+0x82b/0x1080
 process_one_work+0x6ea/0x1190
 worker_thread+0x45b/0xc90
 kthread+0x2d9/0x3c0
 ret_from_fork+0x380/0x5b0
 ret_from_fork_asm+0x1a/0x30

The buggy address belongs to the object at ffff88800df07200
                which belongs to the cache kmalloc-64 of size 64
The buggy address is located 0 bytes inside of
                allocated 64-byte region [ffff88800df07200, ffff88800df07240)

The buggy address belongs to the physical page:
page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0xdf07
flags: 0x100000000000000(node=0|zone=1)
page_type: f5(slab)
raw: 0100000000000000 ffff8880074418c0 dead000000000100 dead000000000122
raw: 0000000000000000 0000000000200020 00000000f5000000 0000000000000000
page dumped because: kasan: bad access detected

Memory state around the buggy address:
 ffff88800df07100: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
 ffff88800df07180: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
>ffff88800df07200: 00 00 00 00 00 00 00 00 fc fc fc fc fc fc fc fc
                                           ^
 ffff88800df07280: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
 ffff88800df07300: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
==================================================================

The front segment doesn't have this problem: mon_alloc_msg() drops
the singleton and allocates a bigger message whenever front_len
exceeds front_alloc_len. Give the middle the same treatment:
reallocate it when it is missing or too small, rather than only when
it is missing. Both messenger versions are covered since they share
ceph_con_in_msg_alloc() -- the WARN_ON() in v2 __handle_control()
should no longer have anything to complain about either.

Fixes: bb257664f748 ("ceph: simplify ceph_msg_new")
Cc: stable@vger.kernel.org
Signed-off-by: Xingyuan Mo <hdthky0@gmail.com>
---
 net/ceph/messenger.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/net/ceph/messenger.c b/net/ceph/messenger.c
index cc7db8c61db37..754d051fe771b 100644
--- a/net/ceph/messenger.c
+++ b/net/ceph/messenger.c
@@ -2098,7 +2098,11 @@ int ceph_con_in_msg_alloc(struct ceph_connection *con,
 	}
 	memcpy(&con->in_msg->hdr, hdr, sizeof(*hdr));
 
-	if (middle_len && !con->in_msg->middle) {
+	if (middle_len &&
+	    (!con->in_msg->middle || middle_len > con->in_msg->middle->alloc_len)) {
+		ceph_buffer_put(con->in_msg->middle);
+		con->in_msg->middle = NULL;
+
 		ret = ceph_alloc_middle(con, con->in_msg);
 		if (ret < 0) {
 			ceph_msg_put(con->in_msg);
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-16 10:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 10:35 [PATCH] libceph: don't reuse a middle buffer that is too small Xingyuan Mo

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®