[PATCH 4/9] ceph: fix mdsmap use-after-free in send_renew_caps() and renewed_caps()

From: Max Kellermann

Date: Wed Nov 11 2026 - 11:20:54 EST


send_renew_caps() and renewed_caps() read mdsc->mdsmap while holding
only session->s_mutex, but ceph_mdsc_handle_mdsmap() replaces the
mdsmap and frees the old one under mdsc->mutex. They are called from
delayed_work() and handle_session(), both of which can run while a
new mdsmap arrives, so they can read the MDS state and the session
timeout from freed memory.

Read those values while holding mdsc->mutex. Callers hold s_mutex
but not mdsc->mutex, and taking mdsc->mutex under s_mutex is the
existing lock order (e.g. check_new_map(), handle_session()).

Fixes: 2f2dc053404f ("ceph: MDS client")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Max Kellermann <max.kellermann@xxxxxxxxx>
---
fs/ceph/mds_client.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c
index da40f2cb80f2..a43ce23d48f4 100644
--- a/fs/ceph/mds_client.c
+++ b/fs/ceph/mds_client.c
@@ -2190,7 +2190,9 @@ static int send_renew_caps(struct ceph_mds_client *mdsc,

/* do not try to renew caps until a recovering mds has reconnected
* with its clients. */
+ mutex_lock(&mdsc->mutex);
state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
+ mutex_unlock(&mdsc->mutex);
if (state < CEPH_MDS_STATE_RECONNECT) {
boutc(cl, "ignoring mds%d (%s)\n", session->s_mds,
ceph_mds_state_name(state));
@@ -2232,14 +2234,19 @@ static void renewed_caps(struct ceph_mds_client *mdsc,
struct ceph_mds_session *session, int is_renew)
{
struct ceph_client *cl = mdsc->fsc->client;
+ u32 session_timeout;
int was_stale;
int wake = 0;

+ mutex_lock(&mdsc->mutex);
+ session_timeout = mdsc->mdsmap->m_session_timeout;
+ mutex_unlock(&mdsc->mutex);
+
spin_lock(&session->s_cap_lock);
was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);

session->s_cap_ttl = session->s_renew_requested +
- mdsc->mdsmap->m_session_timeout*HZ;
+ session_timeout * HZ;

if (was_stale) {
if (time_before(jiffies, session->s_cap_ttl)) {
--
2.53.0