[PATCH 3/9] ceph: fix mdsmap use-after-free in __ceph_setxattr()

From: Max Kellermann

Date: Wed Nov 11 2026 - 11:28:10 EST


__ceph_setxattr() reads mdsc->mdsmap->m_max_xattr_size while holding
only ci->i_ceph_lock, but ceph_mdsc_handle_mdsmap() replaces the
mdsmap and frees the old one under mdsc->mutex. A setxattr() racing
with an mdsmap update can therefore read freed memory.

mdsc->mutex cannot be taken under i_ceph_lock, so keep a copy of the
limit in struct ceph_mds_client, updated by ceph_mdsc_handle_mdsmap()
under the mutex, and read that instead.

Fixes: d93231a6bc8a ("ceph: prevent a client from exceeding the MDS maximum xattr size")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Max Kellermann <max.kellermann@xxxxxxxxx>
---
fs/ceph/mds_client.c | 1 +
fs/ceph/mds_client.h | 7 +++++++
fs/ceph/xattr.c | 10 ++++++----
3 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c
index 15df9d22008d..da40f2cb80f2 100644
--- a/fs/ceph/mds_client.c
+++ b/fs/ceph/mds_client.c
@@ -7247,6 +7247,7 @@ void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
} else {
mdsc->mdsmap = newmap; /* first mds map */
}
+ WRITE_ONCE(mdsc->max_xattr_size, mdsc->mdsmap->m_max_xattr_size);
mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
MAX_LFS_FILESIZE);

diff --git a/fs/ceph/mds_client.h b/fs/ceph/mds_client.h
index ff9ba09f3b59..bd97f73ede20 100644
--- a/fs/ceph/mds_client.h
+++ b/fs/ceph/mds_client.h
@@ -495,6 +495,13 @@ struct ceph_mds_client {
struct mutex mutex; /* all nested structures */

struct ceph_mdsmap *mdsmap;
+
+ /*
+ * Copy of mdsmap->m_max_xattr_size for readers that cannot take
+ * the mutex; updated by ceph_mdsc_handle_mdsmap().
+ */
+ u64 max_xattr_size;
+
struct completion safe_umount_waiters;
wait_queue_head_t session_close_wq;
struct list_head waiting_for_map;
diff --git a/fs/ceph/xattr.c b/fs/ceph/xattr.c
index c6085191c29a..8f97b6448d7a 100644
--- a/fs/ceph/xattr.c
+++ b/fs/ceph/xattr.c
@@ -1211,6 +1211,7 @@ int __ceph_setxattr(struct inode *inode, const char *name,
struct ceph_mds_client *mdsc = fsc->mdsc;
struct ceph_cap_flush *prealloc_cf = NULL;
struct ceph_buffer *old_blob = NULL;
+ u64 max_xattr_size;
int issued;
int err;
int dirty = 0;
@@ -1260,13 +1261,14 @@ int __ceph_setxattr(struct inode *inode, const char *name,

spin_lock(&ci->i_ceph_lock);
retry:
+ max_xattr_size = READ_ONCE(mdsc->max_xattr_size);
issued = __ceph_caps_issued(ci, NULL);
required_blob_size = __get_required_blob_size(ci, name_len, val_len);
if ((ci->i_xattrs.version == 0) || !(issued & CEPH_CAP_XATTR_EXCL) ||
- (required_blob_size > mdsc->mdsmap->m_max_xattr_size)) {
+ (required_blob_size > max_xattr_size)) {
boutc(cl, "sync version: %llu size: %d max: %llu\n",
ci->i_xattrs.version, required_blob_size,
- mdsc->mdsmap->m_max_xattr_size);
+ max_xattr_size);
goto do_sync;
}

@@ -1294,9 +1296,9 @@ int __ceph_setxattr(struct inode *inode, const char *name,
* prealloc_blob size check below reflects the current tree state.
*/
required_blob_size = __get_required_blob_size(ci, name_len, val_len);
- if (required_blob_size > mdsc->mdsmap->m_max_xattr_size) {
+ if (required_blob_size > max_xattr_size) {
boutc(cl, "sync (size too large): %d > %llu\n",
- required_blob_size, mdsc->mdsmap->m_max_xattr_size);
+ required_blob_size, max_xattr_size);
goto do_sync;
}

--
2.53.0