fs/ceph: MDS connections can deliver CEPH_MSG_MDS_MAP and re-enter ceph_mdsc_handle_mdsmap() (UAF)

From: 徐晗翔

Date: Tue Nov 17 2026 - 07:53:46 EST


mds_dispatch() dispatches CEPH_MSG_MDS_MAP from any connection
(mds_client.c:7215-7216) and mds_alloc_msg() allocates an skb for any
message type (7384-7403); ceph_con_process_message() calls ->dispatch()
after dropping con->mutex (messenger.c:1413/1424), so two connection works
can run ceph_mdsc_handle_mdsmap() concurrently. Inside that function
check_new_map() drops mdsc->mutex at 5881/5900/5919/5941/5991 while
mdsc->mdsmap already points at this writer's newmap; a second writer that
passes the epoch check publishes its own map and frees the first writer's
newmap as oldmap (7061), and the first writer resumes using its local newmap
and the published pointer -> use-after-free. A malicious or defective MDS
can drive this deterministically: it controls the epoch (decoded from the
map, mdsmap.c:148; filtered at mds_client.c:7044) and can supply a map that
forces the first writer into an unlocked window. This is a protocol
hardening issue (no connection-type/msg-type validation) on top of the same
missing RCU/refcount lifetime; I have not reproduced it dynamically.

The detailed description:

* Test environment / affected version

Linux 7.3.0 tree (Makefile: VERSION 7, PATCHLEVEL 3, SUBLEVEL 0) at
/home/xuhanxiang/project/race/linux, not a git checkout:

fs/ceph/mds_client.c 8678459253e5b1452667946bf4457c5a
fs/ceph/mdsmap.c 5cb2b54f0d3007961c36e68da1f6318c
net/ceph/messenger.c 87b3dea680e86b1e2dd0cc901004178f

All line numbers are from this tree. Static analysis only; no dynamic
reproduction for this one.

* Mechanism

mds_dispatch() (mds_client.c:7198-7253) validates the session, then:

switch (type) {
case CEPH_MSG_MDS_MAP:
ceph_mdsc_handle_mdsmap(mdsc, msg);
break;
...

There is no check that the message arrived on the mon connection (the
normal path is extra_mon_dispatch(), super.c:795-807) rather than on an MDS
connection. mds_alloc_msg() (mds_client.c:7384-7403) allocates for any
type, and ceph_con_process_message() (messenger.c:1408-1427) releases
con->mutex at 1413 before calling con->ops->dispatch() at 1424, so dispatch
(and therefore ceph_mdsc_handle_mdsmap()) runs without con->mutex.

ceph_mdsc_handle_mdsmap() then:

mutex_lock(&mdsc->mutex);
if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) { ... return; }
newmap = ceph_mdsmap_decode(...);
if (mdsc->mdsmap) {
oldmap = mdsc->mdsmap;
mdsc->mdsmap = newmap;
check_new_map(mdsc, newmap, oldmap); /* drops mutex! */
ceph_mdsmap_destroy(oldmap);
}

check_new_map() unlocks mdsc->mutex at 5881/5900/5919/5941/5991 while
mdsc->mdsmap == newmap (W1's map) and W1 keeps using its local newmap
afterwards (5867/5967/6005/6011). A second writer W2 that acquires the
mutex publishes its own map and, because mdsc->mdsmap is W1's newmap,
frees it as oldmap at 7061. W1 then resumes and dereferences freed memory
(its local newmap, and mdsc->mdsmap at 7065/7068/7070).

Epoch control: the map epoch is decoded from the message payload
(mdsmap.c:148) and only compared with mdsc->mdsmap->m_epoch
(mds_client.c:7044-7048); a malicious MDS knows the current epoch and can
send epoch+1. To force W1 into an unlocked window, the crafted map can,
for example, set possible_max_rank smaller than an existing session rank
(the 5876 branch) so check_new_map() takes the branch that drops the mutex.

* Trigger model

- Requires a malicious, compromised or defective MDS (or a peer able to
send MDS-map messages on an MDS connection). This is not reachable by a
normal client and not by unprivileged local users.
- If it happens, the consequence is kernel memory corruption (UAF write/
read) in the client, i.e. oops/DoS.

* Reproducer

None attached: this report is static-only. The dynamic harness used for the
other reports cannot reach the MDS connection dispatch path without a live
MDS session, and constructing a malicious MDS was out of scope.


* Suggested direction

Reject CEPH_MSG_MDS_MAP (and FS_MAP_USER) on non-mon connections in
mds_dispatch(), or validate in mds_alloc_msg() that the message type is
allowed for the connection; and publish mdsc->mdsmap with RCU so a writer
that drops mdsc->mutex inside check_new_map() cannot have its newmap freed
by another writer.

Please keep me in Cc; I am not subscribed to the lists.

Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx