Skip to contentSkip to conversation

libceph: bound sparse-read consumption to received data in secure mode

PATCH Superseded Dairui Zhang 2 participants 4 messages Sep 20 – Oct 6 Ceph common code (libceph)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libceph: bound sparse-read consumption to received data in secure mode

In msgr2 secure mode the whole reply tail is read into a page vector sized by the preamble-declared tail_len:

        enc_page_cnt = calc_pages_for(0, tail_len);

tail_len comes from the peer: preamble parsing only enforces fd_lens[3] <= CEPH_MSG_MAX_DATA_LEN, and for sparse replies the data_len sanity check is skipped entirely (osd_client.c:5458, gated on !srlen).

process_v2_sparse_read() then walks that vector by server-supplied extent lengths with no bounds check:

        int idx = spos >> PAGE_SHIFT;
        struct page *spage = con->v2.in_enc_pages[idx];

sparse_extent_map_valid() only requires the extents to be ordered, non-overlapping, inside the *requested* range, sum(len) == sr_datalen, and each len <= INT_MAX. So an OSD can send a tiny tail with a perfectly valid extent map claiming data that was never sent, and nothing notices. (The destination cursor doesn't clamp this
either: sparse_read_total is the sum of the *request's* op lengths, and the map validation forces sum(len) <= sr_req_len, so the destination can't run out before the source overruns.)

With a malicious or compromised OSD - the same model as CVE-2026-89657 - the copy loop runs off the end of the page vector:

- past in_enc_page_cnt, adjacent slab gets dereferenced as struct

    page * -> near-certain oops;

- even before that, ceph_alloc_page_vector() doesn't zero its pages, so the tail-page slack is stale kernel memory that gets memcpy_page()d into the user's read() buffer every time;
- if a wild page * happens to be dereferenceable, adjacent heap keeps flowing into read() too.

Plain (crc) mode doesn't have the problem - it pulls from the socket on demand and bails with -EIO past cursor->total_resid.

Trigger: cephfs with ms_mode=secure, sparse path taken via the sparseread mount option or fscrypt-encrypted inodes (fs/ceph/file.c:1079,1541). I didn't check whether rbd issues SPARSE_READ, so no claim there.

This isn't CVE-2026-89657: that one validated the map against the request range to avoid the BUG_ON, it doesn't cover the secure-mode supply/consumption mismatch. Unchanged in mainline as of today.

Reject consumption beyond the received data. Guarding idx covers both the metadata and the data copies.

Fixes f36217e35ce1 ("libceph: support sparse reads on msgr2 secure codepath")

---
First time posting to ceph-devel, and this is from code reading rather than a live cluster - if I've misread the messenger flow or the guard belongs somewhere else, please say so and I'll respin.

 net/ceph/messenger_v2.c | 4 ++++
 1 file changed, 4 insertions(+)
net/ceph/messenger_v2.c +4 -0
index b323b61..c677ff8 100644
@@ -1077,6 +1077,10 @@ static int process_v2_sparse_read(struct ceph_connection *con,
 		do {
 			int idx = spos >> PAGE_SHIFT;
 			int soff = offset_in_page(spos);
+
+			if (idx >= con->v2.in_enc_page_cnt)
+				return -EREMOTEIO;
+
 			struct page *spage = con->v2.in_enc_pages[idx];
 			int len = min_t(int, ret, PAGE_SIZE - soff);
 

If there's interest I'll follow up with two small patches: zeroing pages in ceph_alloc_page_vector() (closes the stale-page leak), and a KUnit case that feeds a short tail plus a large extent map into process_v2_sparse_read().

maintainer
#1 lore raw

Hi Dairui,

NACK. Please respin.

1. net/ceph/messenger_v2.c:1125: idx guard still copies last-page slack into a successful read Dairui, idx >= in_enc_page_cnt stops the OOB in_enc_pages[idx]
walk, but it is not a bound on received bytes.
Cap against the decrypted data segment, not the page count.

Signature · 1 line
Alex Markuze
#2 lore raw

PATCH v2 1/1 libceph: bound sparse-read consumption to received data in secure mode

In reply to Dairui Zhang

Hi Alex,

You're right - my v1 only covered the OOB page walk, and the last-page slack could still be read into a successful reply. Sorry for missing that and for the extra round of review.

v2 now caps against the decrypted data segment (spos + data_len): any chunk that would pass it gets -EREMOTEIO before the page is even dereferenced.

From f36217e35ce1a6f0f7c8c0f9e8f8f8f8f8f8f8f8f Mon Sep 17 00:00:00 2001
From: Dairui Zhang <[email protected]>
Date: Thu, 02 Oct 2026 10:30:00 +0800
Subject: [PATCH v2] libceph: bound sparse-read consumption to received data in secure mode

In msgr2 secure mode the whole reply tail is read into a page vector sized by the preamble-declared tail_len:

        enc_page_cnt = calc_pages_for(0, tail_len);

tail_len comes from the peer: preamble parsing only enforces fd_lens[3] <= CEPH_MSG_MAX_DATA_LEN, and for sparse replies the data_len sanity check is skipped entirely (osd_client.c:5458, gated on !srlen).

process_v2_sparse_read() then walks that vector by server-supplied extent lengths with no bounds check:

        int idx = spos >> PAGE_SHIFT;
        struct page *spage = con->v2.in_enc_pages[idx];

sparse-read reply validation only requires the extents to be ordered, non-overlapping, inside the *requested* range, sum(len) == sr_datalen, and each len <= INT_MAX. So an OSD can send a tiny tail with a perfectly valid extent map claiming data that was never sent, and nothing notices. (The destination cursor doesn't clamp this either: sparse_read_total is the sum of the *request's* op lengths, and the map validation forces sum(len) <= sr_req_len, so the destination can't run out before the source overruns.)

With a malicious or compromised OSD - the same model as CVE-2026-89657 - the copy loop runs off the end of the page vector:

- past in_enc_page_cnt, adjacent slab gets dereferenced as struct

    page * -> near-certain oops;

- even before that, ceph_alloc_page_vector() doesn't zero its pages, so the tail-page slack is stale kernel memory that gets memcpy_page()d into the user's read() buffer every time;
- if a wild page * happens to be dereferenceable, adjacent heap keeps flowing into read() too.

Plain (crc) mode doesn't have the problem - it pulls from the socket on demand and bails with -EIO past cursor->total_resid.

Trigger: cephfs with ms_mode=secure, sparse path taken via the sparseread mount option or fscrypt-encrypted inodes (fs/ceph/file.c:1079,1541). I didn't check whether rbd issues SPARSE_READ, so no claim there.

This isn't CVE-2026-89657: that one validated the map against the request range to avoid the BUG_ON, it doesn't cover the secure-mode supply/consumption mismatch. Unchanged in mainline as of today.

Reject consumption beyond the decrypted data segment: the copy loop now returns -EREMOTEIO as soon as a chunk would pass spos + data_len, not just at the end of the page vector, so it covers both the out-of-bounds page walk and the last-page slack.

Fixes f36217e35ce1 ("libceph: support sparse reads on msgr2 secure codepath")

---
v1 -> v2:
- Cap against the decrypted data segment (spos + data_len) instead
of the page count, per Alex Markuze's review: the page-count
guard stopped the OOB page walk but still let a reply read
last-page slack into a successful read.
- v1: https://lore.kernel.org/ceph-devel/[email protected]/

This is from code reading, not a live cluster.

---
 net/ceph/messenger_v2.c | 7 +++++++
 1 file changed, 6 insertions(+), 1 deletions(-)
net/ceph/messenger_v2.c +6 -1
@@ -1060,6 +1060,7 @@
 				  struct page **pages, int spos)
 {
 	struct ceph_msg_data_cursor cursor;
+	int data_end = spos + (int)data_len(con->in_msg);
 	int ret;
 
 	ceph_msg_data_cursor_init(&cursor, con->in_msg,
@@ -1077,9 +1078,13 @@
 		do {
 			int idx = spos >> PAGE_SHIFT;
 			int soff = offset_in_page(spos);
-			struct page *spage = con->v2.in_enc_pages[idx];
 			int len = min_t(int, ret, PAGE_SIZE - soff);
+			struct page *spage;
 
+			if (spos + len > data_end)
+				return -EREMOTEIO;
+
+			spage = con->v2.in_enc_pages[idx];
 			if (buf) {
 				memcpy_from_page(buf, spage, soff, len);
 				buf += len;
Signature · 1 line
2.53.0