Skip to content

ceph: don't unregister an MDS session before removing its caps

Max Kellermann ceph-devel lkml stable 4 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 Latest3 patches 15 repliesIn review
  2. v33 patches 2 repliesSuperseded
  3. v21 patch 1 replySuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v11 patch1 replySuperseded

Changes in v4 from the cover letter

- replace "ceph/mds_client: pin sessions while checking a new MDS map" which was meanwhile superseded by commit ee611a750955 ("ceph: fix UAF in check_new_map() on session freed during unlock") with one that fixes the remaining UAF bugs
- adjust ceph_mdsc_reset_workfn()
- serialize send_mds_reconnect() state transitions with session CLOSE and prevented reconnect failure rollback from overwriting CLOSED.

Patches in v4 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3ceph: fix use-after-free in check_new_map() after early session putfs/ceph/mds_client.c+4 -91 reply
2/3ceph: stop checking a stale MDS map after dropping mutexfs/ceph/mds_client.c+19 -01 reply
3/3ceph: don't unregister an MDS session before removing its capsfs/ceph/caps.c fs/ceph/mds_client.c+156 -304 replies

Reviews trailers per patch

All 3 patches have a Reviewed-by or Acked-by.

PatchXiubo Li
1/3R-b
2/3R-b
3/3R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v4 thread

Cover letter [PATCH v4 0/3] ceph: don't unregister an MDS session before removing its caps

handle_session() removed the session from mdsc->sessions[] at the very top of CEPH_SESSION_CLOSE handling, before taking s_mutex. Between session unregistration and remove_session_caps(), the MDS rank has no registered session while the old session still owns all caps it was granted.

Any concurrent filesystem operation may walk into that and the next __do_request() call registers a new session for this rank. Once it is open and the MDS issues caps, ceph_fill_inode() calls ceph_add_cap(), which looks caps up by rank, not
by session identity, finding old caps linked to the old session.

The list_move_tail() call then moves the cap object to the new session, which is already a bad thing to do. Since it doesn't decrement old_session->s_nr_caps, this quickly triggers:

kernel BUG at fs/ceph/mds_client.c:1959!

  Internal error: Oops - BUG: 00000000f2000800 [#1]  SMP
  [...]
  Workqueue: ceph-msgr ceph_con_workfn
  pstate: 20400009 (nzCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
  pc : remove_session_caps+0x2bc/0x2d8
  lr : remove_session_caps+0x74/0x2d8
  [...]
  Call trace:
   remove_session_caps+0x2bc/0x2d8 (P)
   mds_dispatch+0xf48/0x1b60
   ceph_con_process_message+0x74/0xa0
   ceph_con_v1_try_read+0x3a0/0x1510
   ceph_con_workfn+0x260/0x460
   process_one_work+0x168/0x3b8
   worker_thread+0x1bc/0x3a0
   kthread+0x118/0x1e0
   ret_from_fork+0x10/0x20

That's BUG_ON(session->s_nr_caps > 0).

I was able to reproduce this reliably by delaying the close and starting I/O during the delay.

This patch keeps the session registered with CEPH_MDS_SESSION_CLOSED. New requests will be put on the s_waiting list where they will be resumed on the new session.

---
v1->v2: skip CLOSED sessions in check_new_map()
v2->v3: split session pinning and stale-map guards into two preparatory
patches; rework CLOSED-session and export-target teardown handling
v3->v4:
- replace "ceph/mds_client: pin sessions while checking a new MDS map" which was meanwhile superseded by commit ee611a750955 ("ceph: fix UAF in check_new_map() on session freed during unlock") with one that fixes the remaining UAF bugs
- adjust ceph_mdsc_reset_workfn()
- serialize send_mds_reconnect() state transitions with session CLOSE and prevented reconnect failure rollback from overwriting CLOSED.

Max Kellermann (3):
ceph: fix use-after-free in check_new_map() after early session put
ceph: stop checking a stale MDS map after dropping mutex
ceph: don't unregister an MDS session before removing its caps

 fs/ceph/caps.c       |   4 +
 fs/ceph/mds_client.c | 214 +++++++++++++++++++++++++++++++++++--------
 2 files changed, 179 insertions(+), 39 deletions(-)
Signature · 1 line
2.47.3