Skip to contentSkip to conversation

ceph: support subvolume quarantine

PATCH Superseded Alex Markuze 3 participants 10 messages Oct 20 – Oct 21

Subsystems (4)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Alex Markuze

author maintainer
#0 lore raw

v5 cover ceph: support subvolume quarantine

CephFS subvolume quarantine isolates a subvolume while allowing recovery clients with q/Q MDS auth caps to access it. Advertise support only after cached data and metadata access paths can enforce quarantine.

This series is based on the testing branch of:
https://github.com/ceph/ceph-client.git
at commit ed0502cb11f84fff29f374238e526ae94d9bdfde ("ceph: validate encoded xattr name lengths"). The repository defaults to for-linus; the base is available on testing.

This series combines the fscrypt symlink-cache prerequisite and the Ceph quarantine series as requested by Eric and Xiubo. The dcache export is retained. The generic fscrypt change precedes the Ceph patches, and its Ceph opt-in is folded into the patch adding the get_link checks.

Changes since v4:
- Add the missing base-commit and identify the public testing branch.
- Require a live, quarantine-aware AUTH_SHARED cap for cached head metadata access. Without it, ask the authoritative MDS for each operation, including cached stat, retained directory replies and encryption-policy/nonce ioctls. A successful reply without a cap authorizes only that operation. The fix is folded into patch 5; the first four patches and public base are unchanged.

Changes since v3:
- Disable only the two fscrypt symlink-cache writers. Leave cache readers unchanged and update fscrypt_symlink_getattr() kernel-doc. Explain the held O_PATH descriptor / empty-path readlinkat() bypass and the allocation and decryption cost of uncached encrypted symlink accesses.
- Avoid unconditional snapshot RPCs only when the connected head inode is the target's actual subvolume root, identified by its nonzero MDS-provided subvolume ID, with a valid AUTH_SHARED cap from a quarantine-aware MDS and no local quarantine marks. A snapshot P/.snap/S/V/file cannot use ancestor P's cap to authorize access in V. Synthetic snapdirs inherit their parent's subvolume ID. Unknown IDs, other head ancestors, expired root caps and disconnected snapshots retain MDS checks. 32-bit clients also revalidate because membership IDs use plain 64-bit READ/WRITE_ONCE accesses.
- Wait interruptibly and retry unstable namespace generations or MDS authorizations in blocking cap acquisition and revalidation. Preserve nonblocking behavior. Track splice owners and reject recursive LSM hooks with EBUSY, including hooks outside MDS reply processing.
- Gate ACL cache suppression and namespace mutation bookkeeping on negotiated quarantine support. Invalidate older cached ACLs when support is first enabled and pair namespace tokens across that transition.
- Remove the impossible NULL d_parent guards raised during VFS review.

Validation:
Ten deterministic source-extraction regression runners pass, including ASan/UBSan/LSan checks of VFS readlink and fscrypt target ownership. A warm same-subvolume-root snapshot harness performs 1000 getattr calls, 1024 read authorizations and 52 snapdir checks without an MDS RPC. A depth-300 harness completes 20000 blocking revalidations with injected namespace splices without EAGAIN. Additional cases cover feature activation, ACL cache invalidation, old-MDS caps, detached snapshots, epoch/rename races, interrupted waits, recovery permissions and stale cap messages. The ancestor snapshot regression fails on the earlier v4 draft and passes with the fix:
zero local marks and a valid outside head cap still require per-operation MDS authorization for reads, cached stat and xattrs. Unknown IDs, non-root head caps and 32-bit clients also retain MDS revalidation. The new head coverage regression reproduces the cached-access bypass before the fix and verifies denial after it, with missing, PIN-only, expired or stale caps. Repeated accesses without a cap each request authorization; a live AUTH_SHARED head cap preserves the zero-RPC cached path.
A fresh complete W=1 kernel and module build passes with GCC 14.2.0 and the normalized x86_64-rhel-9.4 robot config: vmlinux, bzImage and all 2233 configured modules, including ceph.ko and libceph.ko. MODPOST succeeds, and the GPL d_find_alias_rcu export resolves. Additional W=1 builds pass on x86-64 with ACL/encryption enabled and disabled, and on i386 with those features enabled. All five patches pass strict checkpatch and apply in order to reproduce the tested source tree.
These are source harnesses, not live CephFS benchmark results. Live cluster, KASAN/lockdep, failover and snapshot throughput tests remain pending. Xiubo's v3 runtime results are separate evidence.

Remaining protocol limits:
Already installed snapshot PTEs still need server notifications to revoke access without a new filesystem callback. Snapshots without proven coverage by a cap on their own subvolume root, and all 32-bit snapshot operations, still authorize each operation through the MDS.
Disable messages carry no generation, so same-rank ordering relies on ordered delivery. Overlapping ordinary rw and q/Q grants still require server-side auth-cap filtering; Xiubo reproduced that with ceph-fuse too.

v4: https://lore.kernel.org/r/[email protected]/
v3: https://lore.kernel.org/r/[email protected]/
v2: https://lore.kernel.org/r/[email protected]/
fscrypt discussion: https://lore.kernel.org/r/a01d0a297a3e174d21741a3ad586ce546327603e.1792081664.git.amarkuze@redhat.com/

Alex Markuze (5):
dcache: export d_find_alias_rcu() for modular filesystems
fscrypt: allow filesystems to disable symlink target caching
ceph: add subvolume quarantine wire definitions
ceph: record subvolume quarantine from MDS cap updates
ceph: fail quarantined subvolume access with EACCES

 fs/ceph/acl.c                |  33 +-
 fs/ceph/addr.c               |  72 ++-
 fs/ceph/caps.c               | 317 +++++++++++-
 fs/ceph/crypto.c             |   1 +
 fs/ceph/debugfs.c            |   1 +
 fs/ceph/dir.c                |  54 +-
 fs/ceph/file.c               | 112 +++-
 fs/ceph/inode.c              | 954 ++++++++++++++++++++++++++++++++++-
 fs/ceph/ioctl.c              |  13 +
 fs/ceph/mds_client.c         |  28 +-
 fs/ceph/mds_client.h         |  30 +-
 fs/ceph/super.c              |   4 +
 fs/ceph/super.h              | 101 +++-
 fs/ceph/xattr.c              |  54 +-
 fs/crypto/hooks.c            |  28 +-
 fs/dcache.c                  |   1 +
 include/linux/ceph/ceph_fs.h |  13 +
 include/linux/fscrypt.h      |   7 +
 net/ceph/ceph_common.c       |   2 +
 19 files changed, 1712 insertions(+), 113 deletions(-)

base-commit: ed0502cb11f84fff29f374238e526ae94d9bdfde

author maintainer
#1 lore raw

PATCH v5 1/5 dcache: export d_find_alias_rcu() for modular filesystems

The Ceph subvolume quarantine ancestry walk needs to find a dentry alias under RCU without taking a reference. d_find_alias_rcu() provides these semantics, but it is not exported, so a modular Ceph client fails at modpost with an undefined reference to the helper.

Export d_find_alias_rcu() to GPL modules so filesystem modules can use the existing RCU lookup without changing its lifetime rules.

---
 fs/dcache.c | 1 +
 1 file changed, 1 insertion(+)
fs/dcache.c +1 -0
index 1b1a81f10da6..13ba553081b3 100644
@@ -1248,6 +1248,7 @@ struct dentry *d_find_alias_rcu(struct inode *inode)
 	spin_unlock(&inode->i_lock);
 	return de;
 }
+EXPORT_SYMBOL_GPL(d_find_alias_rcu);
 
 /*
  *	Try to kill dentries associated with this inode.
author maintainer
#3 lore raw

PATCH v5 2/5 fscrypt: allow filesystems to disable symlink target caching

In reply to Alex Markuze

fscrypt caches decrypted symlink targets in inode->i_link. The VFS can return that target without calling the filesystem's ->get_link(), so access checks in that callback cannot revoke a previously cached link. CephFS subvolume quarantine needs those checks even when readlinkat() uses an empty path on an O_PATH descriptor opened before quarantine.

Add a filesystem opt-out that prevents both creation-time and lookup- time cache installation. Leave cache readers unchanged: the option is fixed before inode creation, so they find no cached target. Uncached targets use the existing delayed-free callback.

Opting out costs a temporary allocation and decryption on each access to an encrypted symlink, including readlink, traversal and lstat. Other filesystems keep their existing caching behavior. The Ceph opt-in is in the quarantine enforcement patch in this series.

---
 fs/crypto/hooks.c       | 28 +++++++++++++++-------------
 include/linux/fscrypt.h |  7 +++++++
 2 files changed, 22 insertions(+), 13 deletions(-)
fs/crypto/hooks.c +15 -13
index a7a8a3f581a0..6bc7ac82ec34 100644
@@ -321,11 +321,13 @@ int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
 	 */
 	sd->encrypted_path[ciphertext_len] = '\0';
 
-	/* Cache the plaintext symlink target for later use by get_link() */
-	err = -ENOMEM;
-	inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
-	if (!inode->i_link)
-		goto err_free_sd;
+	/* Cache the plaintext target unless the filesystem revalidates it. */
+	if (!inode->i_sb->s_cop->no_symlink_cache) {
+		err = -ENOMEM;
+		inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
+		if (!inode->i_link)
+			goto err_free_sd;
+	}
 
 	if (!disk_link->name)
 		disk_link->name = (unsigned char *)sd;
@@ -411,12 +413,12 @@ const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
 	pstr.name[pstr.len] = '\0';
 
 	/*
-	 * Cache decrypted symlink targets in i_link for later use.  Don't cache
-	 * symlink targets encoded without the key, since those become outdated
-	 * once the key is added.  This pairs with the READ_ONCE() above and in
-	 * the VFS path lookup code.
+	 * Cache decrypted targets unless the filesystem revalidates them.
+	 * Don't cache targets encoded without the key, since those become
+	 * outdated once the key is added. This pairs with the READ_ONCE()
+	 * above and in the VFS path lookup code.
 	 */
-	if (!has_key ||
+	if (inode->i_sb->s_cop->no_symlink_cache || !has_key ||
 	    cmpxchg_release(&inode->i_link, NULL, pstr.name) != NULL)
 		set_delayed_call(done, kfree_link, pstr.name);
 
@@ -442,9 +444,9 @@ EXPORT_SYMBOL_GPL(fscrypt_get_symlink);
  * This requires reading the symlink target from disk if needed, setting up the
  * inode's encryption key if possible, and then decrypting or encoding the
  * symlink target.  This makes lstat() more heavyweight than is normally the
- * case.  However, decrypted symlink targets will be cached in ->i_link, so
- * usually the symlink won't have to be read and decrypted again later if/when
- * it is actually followed, readlink() is called, or lstat() is called again.
+ * case. Unless the filesystem sets no_symlink_cache, decrypted targets
+ * are cached in ->i_link, avoiding another read and decryption when the
+ * symlink is followed, readlink() is called, or lstat() is called again.
  *
  * Return: 0 on success, -errno on failure
  */
include/linux/fscrypt.h +7 -0
index 28f2108e29f3..ab77e685c75c 100644
@@ -107,6 +107,13 @@ struct fscrypt_operations {
 	 */
 	unsigned int supports_subblock_data_units : 1;
 
+	/*
+	 * Don't populate ->i_link, so every access calls ->get_link() and the
+	 * filesystem can revalidate access. Set before creating any inodes;
+	 * this option cannot invalidate previously cached targets.
+	 */
+	unsigned int no_symlink_cache : 1;
+
 	/*
 	 * This field exists only for backwards compatibility reasons and should
 	 * only be set by the filesystems that are setting it already.  It
author maintainer
#4 lore raw

PATCH v5 3/5 ceph: add subvolume quarantine wire definitions

In reply to Alex Markuze

The MDS revokes a quarantined subvolume's caps to PIN and reports -CEPH_EQUARANTINED in MClientCaps v14. This wire errno is 1000 and must be translated to EACCES before returning it to applications. When quarantine is lifted, MQuarantineDisable identifies the inode whose local mark can be cleared.

Add the errno, message type and packed disable payload, and teach ceph_msg_type_name() the message name. Advertise the session feature only after the client can enforce quarantine on cached access.

---
 include/linux/ceph/ceph_fs.h | 13 +++++++++++++
 net/ceph/ceph_common.c       |  2 ++
 2 files changed, 15 insertions(+)
include/linux/ceph/ceph_fs.h +13 -0
index d70bbce1a1b2..5afccfa891cb 100644
@@ -154,6 +154,7 @@ const char *ceph_con_mode_name(int mode);
 #define CEPH_MSG_CLIENT_SNAP            0x312
 #define CEPH_MSG_CLIENT_CAPRELEASE      0x313
 #define CEPH_MSG_CLIENT_QUOTA           0x314
+#define CEPH_MSG_CLIENT_QUARANTINE_DISABLE 0x315
 
 /* pool ops */
 #define CEPH_MSG_POOLOP_REPLY           48
@@ -921,4 +922,16 @@ struct ceph_mds_quota {
 	__le64 max_files;	/* quota max. files */
 } __attribute__ ((packed));
 
+/*
+ * Cap oserrno used by the MDS when a subvolume is quarantined.
+ * This is not a VFS errno; applications see -EACCES.
+ */
+#define CEPH_EQUARANTINED	1000
+
+/* MDS -> client: quarantine lifted for one inode (ino, oserrno). */
+struct ceph_mds_quarantine_disable {
+	__le64 ino;
+	__le32 oserrno;
+} __packed;
+
 #endif
net/ceph/ceph_common.c +2 -0
index 67e88f30409d..cc3d21fd7d5b 100644
@@ -83,6 +83,8 @@ const char *ceph_msg_type_name(int type)
 	case CEPH_MSG_CLIENT_CAPS: return "client_caps";
 	case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
 	case CEPH_MSG_CLIENT_QUOTA: return "client_quota";
+	case CEPH_MSG_CLIENT_QUARANTINE_DISABLE:
+		return "client_quarantine_disable";
 	case CEPH_MSG_CLIENT_SNAP: return "client_snap";
 	case CEPH_MSG_CLIENT_LEASE: return "client_lease";
 	case CEPH_MSG_POOLOP_REPLY: return "poolop_reply";
author maintainer
#5 lore raw

PATCH v5 4/5 ceph: record subvolume quarantine from MDS cap updates

In reply to Alex Markuze

MClientCaps v14 appends a signed quarantine errno after the v13 subvolume ID. Decode it and keep a sticky, counted mark on the inode. A PIN-only update can reset the errno without clearing that mark. An accepted grant above PIN or a disable from the reporting rank clears it. A fresh metadata request can also confirm recovery across ranks. Ignore older cap sequences when changing quarantine state.

Retain a pending mark when the reporting cap or session is lost, so that later access must ask the MDS rather than reuse cached caps. Wake cap waiters when quarantine state changes.

Skip ancestry checks when no inode is marked. Otherwise cache complete RCU parent walks by generation; incomplete walks remain unknown and require an MDS answer. Bound retries and take no inode or dentry references during the walk. Invalidate generations across renames and alias attachments. Sleeping splices disable cached answers without leaving the generation odd across an LSM callback.

This patch records state without advertising quarantine support. The following patch adds the access checks before enabling it.

---
 fs/ceph/caps.c       | 122 ++++++++++-
 fs/ceph/dir.c        |   9 +
 fs/ceph/file.c       |   2 +
 fs/ceph/inode.c      | 476 ++++++++++++++++++++++++++++++++++++++++++-
 fs/ceph/mds_client.c |  23 ++-
 fs/ceph/mds_client.h |  16 ++
 fs/ceph/super.c      |   2 +
 fs/ceph/super.h      |  71 ++++++-
 8 files changed, 713 insertions(+), 8 deletions(-)
fs/ceph/caps.c +121 -1
index b27bcdc24d29..94f015112942 100644
@@ -692,8 +692,9 @@ void change_auth_cap_ses(struct ceph_inode_info *ci,
  * @fmode is the open file mode, if we are opening a file, otherwise
  * it is < 0.  (This is so we can atomically add the cap and add an
  * open file reference to it.)
+ * Returns false when the raw grant predates the recorded cap sequence.
  */
-void ceph_add_cap(struct inode *inode,
+bool ceph_add_cap(struct inode *inode,
 		  struct ceph_mds_session *session, u64 cap_id,
 		  unsigned issued, unsigned wanted,
 		  unsigned seq, unsigned mseq, u64 realmino, int flags,
@@ -706,6 +707,7 @@ void ceph_add_cap(struct inode *inode,
 	int mds = session->s_mds;
 	int actual_wanted;
 	u32 gen;
+	bool cap_current = true;
 
 	lockdep_assert_held(&ci->i_ceph_lock);
 
@@ -737,6 +739,7 @@ void ceph_add_cap(struct inode *inode,
 		atomic64_inc(&mdsc->metric.total_caps);
 		spin_unlock(&session->s_cap_lock);
 	} else {
+		cap_current = ceph_seq_cmp(seq, cap->seq) >= 0;
 		spin_lock(&session->s_cap_lock);
 		list_move_tail(&cap->session_caps, &session->s_caps);
 		spin_unlock(&session->s_cap_lock);
@@ -823,6 +826,7 @@ void ceph_add_cap(struct inode *inode,
 	cap->mseq = mseq;
 	cap->cap_gen = gen;
 	wake_up_all(&ci->i_cap_wq);
+	return cap_current;
 }
 
 /*
@@ -1264,6 +1268,11 @@ static void __ceph_remove_cap(struct ceph_inode_info *ci, struct ceph_cap *cap,
 
 	mdsc = ceph_inode_to_fs_client(&ci->netfs.inode)->mdsc;
 
+	/* Losing the reporting cap requires fresh MDS revalidation. */
+	if (READ_ONCE(ci->i_qtine_mds) == cap->mds &&
+	    ceph_quarantine_reset(ci))
+		wake_up_all(&ci->i_cap_wq);
+
 	/* remove from inode's cap rbtree, and clear auth cap */
 	rb_erase(&cap->ci_node, &ci->i_caps);
 	if (ci->i_auth_cap == cap)
@@ -3729,6 +3738,9 @@ struct cap_extra_info {
 	u8 *fscrypt_auth;
 	u32 fscrypt_auth_len;
 	u64 fscrypt_file_size;
+	/* MClientCaps v14 oserrno; valid only when oserrno_valid is set */
+	s32 oserrno;
+	bool oserrno_valid;
 };
 
 /*
@@ -3757,6 +3769,45 @@ static int parse_inline_data(void **p, void *end,
 	return -EIO;
 }
 
+/*
+ * Record quarantine state carried by an MClientCaps oserrno.
+ * Caller holds i_ceph_lock. Returns true when waiters should be woken
+ * because quarantine changed or a pending reset was resolved.
+ *
+ * -EQUARANTINED refreshes the sticky flag and its reporting MDS rank.
+ * A current grant above PIN with errno 0 clears only that rank's mark.
+ * Another rank needs a successful metadata request for the current epoch.
+ * A PIN-only update may zero qtine_errno while leaving the flag set.
+ */
+static bool ceph_apply_quarantine(struct ceph_inode_info *ci, int mds,
+				  int newcaps, int oserrno)
+{
+	bool was, now, pending;
+
+	was = READ_ONCE(ci->i_qtine_errno) == -CEPH_EQUARANTINED ||
+	      test_bit(CEPH_I_QUARANTINE_BIT, &ci->i_ceph_flags);
+	pending = test_bit(CEPH_I_QUARANTINE_PENDING_BIT, &ci->i_ceph_flags);
+
+	if ((newcaps & ~CEPH_CAP_PIN) && was && oserrno == 0 &&
+	    READ_ONCE(ci->i_qtine_mds) == mds)
+		ceph_quarantine_clear(ci);
+
+	if (oserrno == -CEPH_EQUARANTINED) {
+		ceph_quarantine_set(ci);
+		WRITE_ONCE(ci->i_qtine_mds, mds);
+	}
+
+	if (newcaps || was ||
+	    test_bit(CEPH_I_QUARANTINE_BIT, &ci->i_ceph_flags))
+		WRITE_ONCE(ci->i_qtine_errno, oserrno);
+
+	now = READ_ONCE(ci->i_qtine_errno) == -CEPH_EQUARANTINED ||
+	      test_bit(CEPH_I_QUARANTINE_BIT, &ci->i_ceph_flags);
+	return was != now ||
+	       pending != test_bit(CEPH_I_QUARANTINE_PENDING_BIT,
+				   &ci->i_ceph_flags);
+}
+
 /*
  * Handle a cap GRANT message from the MDS.  (Note that a GRANT may
  * actually be a revocation if it specifies a smaller cap set.)
@@ -3781,6 +3832,7 @@ static void handle_cap_grant(struct inode *inode,
 	u64 max_size = le64_to_cpu(grant->max_size);
 	unsigned char check_caps = 0;
 	bool was_stale = cap->cap_gen < atomic_read(&session->s_cap_gen);
+	bool quarantine_current = ceph_seq_cmp(seq, cap->seq) >= 0;
 	bool wake = false;
 	bool writeback = false;
 	bool queue_trunc = false;
@@ -4128,6 +4180,15 @@ static void handle_cap_grant(struct inode *inode,
 			fill_inline = true;
 	}
 
+	if (quarantine_current && extra_info->oserrno_valid &&
+	    ceph_apply_quarantine(ci, session->s_mds, newcaps,
+				  extra_info->oserrno)) {
+		boutc(cl, "quarantine oserrno %d caps %s %llx.%llx\n",
+		      extra_info->oserrno, ceph_cap_string(newcaps),
+		      ceph_vinop(inode));
+		wake = true;
+	}
+
 	if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
 		if (ci->i_auth_cap == cap) {
 			if (newcaps & ~extra_info->issued)
@@ -4826,6 +4887,20 @@ void ceph_handle_caps(struct ceph_mds_session *session,
 			goto bad;
 	}
 
+	if (msg_version >= 13) {
+		/* v13: subvolume_id. The inode id is learned from replies. */
+		ceph_decode_skip_64(&p, end, bad);
+	}
+
+	if (msg_version >= 14) {
+		u32 raw_errno;
+
+		/* v14: signed oserrno, -CEPH_EQUARANTINED when quarantined */
+		ceph_decode_32_safe(&p, end, raw_errno, bad);
+		extra_info.oserrno = (s32)raw_errno;
+		extra_info.oserrno_valid = true;
+	}
+
 	/* lookup ino */
 	inode = ceph_find_inode(mdsc->fsc->sb, vino);
 	boutc(cl, " caps mds%d op %s ino %llx.%llx inode %p seq %u iseq %u mseq %u\n",
@@ -4981,6 +5056,51 @@ void ceph_handle_caps(struct ceph_mds_session *session,
 	goto out;
 }
 
+void ceph_handle_quarantine_disable(struct ceph_mds_client *mdsc,
+				    struct ceph_mds_session *session,
+				    struct ceph_msg *msg)
+{
+	struct super_block *sb = mdsc->fsc->sb;
+	struct ceph_client *cl = mdsc->fsc->client;
+	struct ceph_mds_quarantine_disable *h = msg->front.iov_base;
+	struct ceph_vino vino;
+	struct inode *inode;
+	struct ceph_inode_info *ci;
+
+	if (!ceph_inc_mds_stopping_blocker(mdsc, session))
+		return;
+
+	if (msg->front.iov_len < sizeof(*h)) {
+		pr_err_client(cl, "corrupt quarantine_disable mds%d len %d\n",
+			      session->s_mds, (int)msg->front.iov_len);
+		ceph_msg_dump(msg);
+		goto out;
+	}
+
+	vino.ino = le64_to_cpu(h->ino);
+	vino.snap = CEPH_NOSNAP;
+	inode = ceph_find_inode(sb, vino);
+	if (!inode) {
+		doutc(cl, "quarantine disable ino %llx not cached\n", vino.ino);
+		goto out;
+	}
+
+	ci = ceph_inode(inode);
+	spin_lock(&ci->i_ceph_lock);
+	/* Another rank's disable must not erase a newer negative. */
+	if (!test_bit(CEPH_I_QUARANTINE_BIT, &ci->i_ceph_flags) ||
+	    READ_ONCE(ci->i_qtine_mds) == session->s_mds) {
+		ceph_quarantine_clear(ci);
+		WRITE_ONCE(ci->i_qtine_errno, 0);
+	}
+	spin_unlock(&ci->i_ceph_lock);
+	wake_up_all(&ci->i_cap_wq);
+	doutc(cl, "quarantine disable ino %llx\n", vino.ino);
+	iput(inode);
+out:
+	ceph_dec_mds_stopping_blocker(mdsc);
+}
+
 /*
  * Process the expired end of one delayed cap list, whose entries were
  * queued @hold jiffies before they expire.
fs/ceph/dir.c +9 -0
index 384e9edabe2a..d8c8b7ad8930 100644
@@ -795,7 +795,9 @@ struct dentry *ceph_handle_snapdir(struct ceph_mds_request *req,
 		struct dentry *res;
 		struct inode *inode = ceph_get_snapdir(parent);
 
+		ceph_quarantine_splice_start(parent->i_sb);
 		res = d_splice_alias(inode, dentry);
+		ceph_quarantine_splice_finish(parent->i_sb);
 		boutc_formats(cl,
 			      "ENOENT on snapdir %p '%s', linking to snapdir %p %llx.%llx. Spliced dentry %p\n",
 			      "ENOENT on snapdir %p '%pd', linking to snapdir %p %llx.%llx. Spliced dentry %p\n",
@@ -1373,7 +1375,9 @@ static int ceph_link(struct dentry *old_dentry, struct inode *dir,
 		d_drop(dentry);
 	} else if (!req->r_reply_info.head->is_dentry) {
 		ihold(d_inode(old_dentry));
+		ceph_quarantine_splice_start(dir->i_sb);
 		d_instantiate(dentry, d_inode(old_dentry));
+		ceph_quarantine_splice_finish(dir->i_sb);
 	}
 	ceph_mdsc_put_request(req);
 	ceph_blog_exit(&__ji);
@@ -1701,12 +1705,17 @@ static int ceph_rename(struct mnt_idmap *idmap, struct inode *old_dir,
 	}
 	err = ceph_mdsc_do_request(mdsc, old_dir, req);
 	if (!err && !req->r_reply_info.head->is_dentry) {
+		struct inode *moved;
+
 		/*
 		 * Normally d_move() is done by fill_trace (called by
 		 * do_request, above).  If there is no trace, we need
 		 * to do it here.
 		 */
+		moved = d_inode(old_dentry);
+		ceph_quarantine_move_start(moved, old_dir, new_dir);
 		d_move(old_dentry, new_dentry);
+		ceph_quarantine_move_finish(moved, old_dir, new_dir);
 	}
 	ceph_mdsc_put_request(req);
 	ceph_blog_exit(&__ji);
fs/ceph/file.c +2 -0
index 856710c63765..9a6eadb94ab4 100644
@@ -785,7 +785,9 @@ static int ceph_finish_async_create(struct inode *dir, struct inode *inode,
 		if (d_in_lookup(dentry) || d_really_is_negative(dentry)) {
 			if (!d_unhashed(dentry))
 				d_drop(dentry);
+			ceph_quarantine_splice_start(dir->i_sb);
 			dn = d_splice_alias(inode, dentry);
+			ceph_quarantine_splice_finish(dir->i_sb);
 			WARN_ON_ONCE(dn && dn != dentry);
 		}
 		file->f_mode |= FMODE_CREATED;
fs/ceph/inode.c +475 -1

542 lines not on this page · raw message

fs/ceph/mds_client.c +22 -1
index d4ef8a355a48..bb7cf9384fd6 100644
@@ -2041,10 +2041,12 @@ static int remove_session_caps_cb(struct inode *inode, int mds, void *arg)
 	struct ceph_inode_info *ci = ceph_inode(inode);
 	struct ceph_client *cl = ceph_inode_to_client(inode);
 	bool invalidate = false;
+	bool reset;
 	struct ceph_cap *cap;
 	int iputs = 0;
 
 	spin_lock(&ci->i_ceph_lock);
+	reset = ceph_quarantine_reset(ci);
 	cap = __get_cap_for_mds(ci, mds);
 	if (cap) {
 		boutc(cl, " removing cap %p, ci is %p, inode is %p\n",
@@ -2054,7 +2056,7 @@ static int remove_session_caps_cb(struct inode *inode, int mds, void *arg)
 	}
 	spin_unlock(&ci->i_ceph_lock);
 
-	if (cap)
+	if (cap || reset)
 		wake_up_all(&ci->i_cap_wq);
 	if (invalidate)
 		ceph_queue_invalidate(inode);
@@ -2860,6 +2862,7 @@ ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
 
 	mutex_init(&req->r_fill_mutex);
 	req->r_mdsc = mdsc;
+	req->r_quarantine_epoch = atomic64_read_acquire(&mdsc->quarantine_epoch);
 	req->r_started = jiffies;
 	req->r_start_latency = ktime_get();
 	req->r_resend_mds = -1;
@@ -5009,6 +5012,7 @@ static int reconnect_caps_cb(struct inode *inode, int mds, void *arg)
 	struct ceph_path_info path_info = {0};
 	int err;
 	u64 snap_follows;
+	bool reset = false;
 
 	dentry = d_find_primary(inode);
 	if (dentry) {
@@ -5023,9 +5027,16 @@ static int reconnect_caps_cb(struct inode *inode, int mds, void *arg)
 	}
 
 	spin_lock(&ci->i_ceph_lock);
+	/*
+	 * Require a fresh MDS answer after reconnect. A PIN import may
+	 * carry no quarantine errno, so it must not reopen cached snap caps.
+	 */
+	reset = ceph_quarantine_reset(ci);
 	cap = __get_cap_for_mds(ci, mds);
 	if (!cap) {
 		spin_unlock(&ci->i_ceph_lock);
+		if (reset)
+			wake_up_all(&ci->i_cap_wq);
 		err = 0;
 		goto out_err;
 	}
@@ -5088,6 +5099,8 @@ static int reconnect_caps_cb(struct inode *inode, int mds, void *arg)
 		snap_follows = capsnap->follows;
 	}
 	spin_unlock(&ci->i_ceph_lock);
+	if (reset)
+		wake_up_all(&ci->i_cap_wq);
 
 	if (recon_state->msg_version >= 2) {
 		int num_fcntl_locks, num_flock_locks;
@@ -6468,6 +6481,11 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc)
 	init_completion(&mdsc->safe_umount_waiters);
 	spin_lock_init(&mdsc->stopping_lock);
 	atomic_set(&mdsc->stopping_blockers, 0);
+	atomic_set(&mdsc->quarantine_count, 0);
+	atomic_set(&mdsc->quarantine_writers, 0);
+	atomic_set(&mdsc->quarantine_splices, 0);
+	atomic64_set(&mdsc->quarantine_epoch, 0);
+	spin_lock_init(&mdsc->quarantine_lock);
 	init_completion(&mdsc->stopping_waiter);
 	atomic64_set(&mdsc->dirty_folios, 0);
 	init_waitqueue_head(&mdsc->flush_end_wq);
@@ -7368,6 +7386,9 @@ static void mds_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
 	case CEPH_MSG_CLIENT_QUOTA:
 		ceph_handle_quota(mdsc, s, msg);
 		break;
+	case CEPH_MSG_CLIENT_QUARANTINE_DISABLE:
+		ceph_handle_quarantine_disable(mdsc, s, msg);
+		break;
 
 	default:
 		pr_err_client(cl, "received unknown message type %d %s\n",
fs/ceph/mds_client.h +16 -0
index 863cdb8d588a..816f5f2d32cc 100644
@@ -375,6 +375,7 @@ struct ceph_mds_request {
 	int r_fmode;        /* file mode, if expecting cap */
 	int r_request_release_offset;
 	const struct cred *r_cred;
+	u64 r_quarantine_epoch; /* generation when the request was created */
 	struct mnt_idmap *r_mnt_idmap;
 	struct timespec64 r_stamp;
 
@@ -506,6 +507,21 @@ struct ceph_mds_client {
 	spinlock_t              stopping_lock;  /* protect snap_empty */
 	int                     stopping;      /* the stage of shutting down */
 	atomic_t                stopping_blockers;
+	/*
+	 * Inodes with CEPH_I_QUARANTINE set. Hot paths skip the
+	 * ancestor walk while this is zero.
+	 * quarantine_epoch is a seqcount: even while idle, odd while
+	 * quarantine_writers is non-zero. Writers are mark changes and
+	 * cross-directory renames. quarantine_lock covers the writers
+	 * update and the epoch bump together. Sleeping alias splices use
+	 * quarantine_splices instead: readers walk without cached answers
+	 * while it is non-zero. Each splice edge also bumps the epoch.
+	 */
+	atomic_t		quarantine_count;
+	atomic_t		quarantine_writers;
+	atomic_t		quarantine_splices;
+	atomic64_t		quarantine_epoch;
+	spinlock_t		quarantine_lock; /* writers update + epoch bump */
 	struct completion	stopping_waiter;
 
 	atomic64_t		dirty_folios;
fs/ceph/super.c +2 -0
index 40be4b16e486..ad9460d9673c 100644
@@ -1139,7 +1139,9 @@ static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
 		struct inode *inode = req->r_target_inode;
 		req->r_target_inode = NULL;
 		doutc(cl, "success\n");
+		ceph_quarantine_splice_start(fsc->sb);
 		root = d_make_root(inode);
+		ceph_quarantine_splice_finish(fsc->sb);
 		if (!root) {
 			root = ERR_PTR(-ENOMEM);
 			goto out;
fs/ceph/super.h +66 -5

95 lines not on this page · raw message

author maintainer
#7 lore raw

PATCH v5 5/5 ceph: fail quarantined subvolume access with EACCES

In reply to Alex Markuze

Advertise CEPHFS_FEATURE_QUARANTINE after gating cached data and metadata access. The MDS can then return EACCES for quarantined requests instead of parking them until quarantine is lifted.

Reject cached access to a directly marked inode. Ask the MDS when ancestry is incomplete or an inherited mark may permit recovery with q/Q auth caps. Scope read confirmations to the exact inode, credentials and epoch; authorize writes separately with a mode- specific OPEN. Keep nonblocking acquisition free of RPCs and drop locked folios before blocking revalidation.

Require a live AUTH_SHARED cap from a quarantine-aware authoritative MDS before using cached head metadata, even with zero local marks. Missing, stale and PIN-only caps cannot deliver the first quarantine revoke. Force an authoritative GETATTR when coverage is absent. A successful reply without a cap authorizes only the current operation; later cached stat, directory and encryption-policy/nonce ioctl accesses must ask again. Retry namespace changes and reject recursive splice hooks before issuing that request.

Wait interruptibly for namespace writers and retry invalidated MDS answers instead of leaking EAGAIN from blocking reads and readdir. Track the tasks performing sleeping splices and reject their recursive LSM hooks with EBUSY rather than waiting on their own operation. Keep the fill-trace recursion guard, including cross-mount calls. Wake waiters when mark writers and splices finish.

Keep snapshot access cached only when the connected head inode is the subvolume root identified by the target's nonzero MDS subvolume ID, holds a valid AUTH_SHARED cap from a quarantine-aware authoritative MDS, and there are no local quarantine marks. An ancestor snapshot P/.snap/S/V/file must not use P's cap to authorize access inside V:
quarantining V leaves P's cap valid and may supply no local marks. Copy the parent's subvolume ID into synthetic snapdir inodes too. Unknown IDs, other head ancestors, expired root caps and active quarantine still require an MDS answer. Keep 32-bit clients on that path because membership IDs use plain 64-bit READ/WRITE_ONCE accesses. Preserve connected paths and exact inode/snapshot LOOKUPINO fallback, including reply-identity and required-payload checks.

Gate VFS ACL cache suppression and namespace mutation bookkeeping on negotiated quarantine support. Invalidate older cached ACLs when the first supporting session opens and pair mutation tokens across that transition. Keep support enabled across subsequent reconnects.

Select fscrypt's symlink cache opt-out alongside the get_link checks. This closes readlinkat() with an empty path on a held O_PATH symlink fd. Pathname lookup already rejects quarantined paths; an existing O_PATH fd bypasses that lookup and needs the get_link check itself. Encrypted symlink readlink, traversal and lstat now allocate and decrypt a temporary target on each access.

Gate cached stat, xattr, directory, ioctl and mmap access. Keep ACL inheritance in the encoded xattr blob and fail ENOMEM if it cannot be installed. Leave metadata submission and lock release on their normal paths, and route uncertain async mutations to the MDS. Fresh metadata replies recover after a lost disable notification. Existing snapshot PTEs still require server notifications for revocation.

---
 fs/ceph/acl.c        |  33 ++-
 fs/ceph/addr.c       |  72 ++++--
 fs/ceph/caps.c       | 195 ++++++++++++++-
 fs/ceph/crypto.c     |   1 +
 fs/ceph/debugfs.c    |   1 +
 fs/ceph/dir.c        |  57 +++--
 fs/ceph/file.c       | 114 +++++++--
 fs/ceph/inode.c      | 556 ++++++++++++++++++++++++++++++++++++++-----
 fs/ceph/ioctl.c      |  13 +
 fs/ceph/mds_client.c |   5 +
 fs/ceph/mds_client.h |  14 +-
 fs/ceph/super.c      |   6 +-
 fs/ceph/super.h      |  42 +++-
 fs/ceph/xattr.c      |  54 ++++-
 14 files changed, 1016 insertions(+), 147 deletions(-)
fs/ceph/acl.c +31 -2
index dbc64ab60308..14feb719a1cf 100644
@@ -18,18 +18,45 @@
 #include "mds_client.h"
 
 static inline void ceph_set_cached_acl(struct inode *inode,
-					int type, struct posix_acl *acl)
+				       int type, struct posix_acl *acl)
 {
 	struct ceph_inode_info *ci = ceph_inode(inode);
 
 	spin_lock(&ci->i_ceph_lock);
-	if (__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 0))
+	if (!READ_ONCE(ceph_sb_to_mdsc(inode->i_sb)->quarantine_supported) &&
+	    __ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 0))
 		set_cached_acl(inode, type, acl);
 	else
 		forget_cached_acl(inode, type);
 	spin_unlock(&ci->i_ceph_lock);
 }
 
+void ceph_quarantine_uncache_inode_acls(struct inode *inode)
+{
+	struct ceph_inode_info *ci = ceph_inode(inode);
+	struct posix_acl *acl, *default_acl;
+
+	spin_lock(&ci->i_ceph_lock);
+	acl = xchg(&inode->i_acl, ACL_DONT_CACHE);
+	default_acl = xchg(&inode->i_default_acl, ACL_DONT_CACHE);
+	spin_unlock(&ci->i_ceph_lock);
+	if (!is_uncached_acl(acl))
+		posix_acl_release(acl);
+	if (!is_uncached_acl(default_acl))
+		posix_acl_release(default_acl);
+}
+
+void ceph_quarantine_disable_acl_cache(struct super_block *sb)
+{
+	struct inode *inode;
+
+	/* Cover inodes cached before the first supporting session opened. */
+	spin_lock(&sb->s_inode_list_lock);
+	list_for_each_entry(inode, &sb->s_inodes, i_sb_list)
+		ceph_quarantine_uncache_inode_acls(inode);
+	spin_unlock(&sb->s_inode_list_lock);
+}
+
 struct posix_acl *ceph_get_acl(struct inode *inode, int type, bool rcu)
 {
 	struct ceph_client *cl = ceph_inode_to_client(inode);
@@ -73,6 +100,8 @@ struct posix_acl *ceph_get_acl(struct inode *inode, int type, bool rcu)
 		acl = posix_acl_from_xattr(&init_user_ns, value, size);
 	} else if (size == -ENODATA || size == 0) {
 		acl = NULL;
+	} else if (size == -EACCES) {
+		acl = ERR_PTR(-EACCES);
 	} else {
 		pr_err_ratelimited_client(cl, "%llx.%llx failed, err=%d\n",
 					  ceph_vinop(inode), size);
fs/ceph/addr.c +56 -16

100 lines not on this page · raw message

fs/ceph/caps.c +186 -9

275 lines not on this page · raw message

fs/ceph/crypto.c +1 -0
index 9e0ebb071840..de786a0e626c 100644
@@ -92,6 +92,7 @@ static struct fscrypt_operations ceph_fscrypt_ops = {
 	.inode_info_offs	= (int)offsetof(struct ceph_inode_info, i_crypt_info) -
 				  (int)offsetof(struct ceph_inode_info, netfs.inode),
 	.needs_bounce_pages	= 1,
+	.no_symlink_cache	= 1,
 	.get_context		= ceph_crypt_get_context,
 	.set_context		= ceph_crypt_set_context,
 	.get_dummy_policy	= ceph_get_dummy_policy,
fs/ceph/debugfs.c +1 -0
index 6a9a5551d9d7..ef10e948a2d3 100644
@@ -53,6 +53,7 @@ static const struct ceph_session_feature_desc ceph_session_feature_table[] = {
 	{ CEPHFS_FEATURE_HAS_OWNER_UIDGID, "HAS_OWNER_UIDGID" },
 	{ CEPHFS_FEATURE_MDS_AUTH_CAPS_CHECK, "MDS_AUTH_CAPS_CHECK" },
 	{ CEPHFS_FEATURE_SUBVOLUME_METRICS, "SUBVOLUME_METRICS" },
+	{ CEPHFS_FEATURE_QUARANTINE, "QUARANTINE" },
 };
 
 static int mdsmap_show(struct seq_file *s, void *p)
fs/ceph/dir.c +39 -18

140 lines not on this page · raw message

fs/ceph/file.c +89 -25

186 lines not on this page · raw message

fs/ceph/inode.c +493 -63

797 lines not on this page · raw message

fs/ceph/ioctl.c +13 -0
index 12cb50e0a166..d152cad10c23 100644
@@ -201,6 +201,10 @@ static long ceph_ioctl_get_dataloc(struct file *file, void __user *arg)
 	if (copy_from_user(&dl, arg, sizeof(dl)))
 		return -EFAULT;
 
+	r = ceph_do_getattr(inode, CEPH_STAT_CAP_LAYOUT, false);
+	if (r)
+		return r;
+
 	down_read(&osdc->lock);
 	ceph_calc_file_object_mapping(&ci->i_layout, dl.file_offset, 1,
 				      &dl.object_no, &dl.object_offset, &xlen);
@@ -401,12 +405,18 @@ long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 
 	case FS_IOC_GET_ENCRYPTION_POLICY:
 		ret = vet_mds_for_fscrypt(file);
+		if (ret)
+			return ret;
+		ret = ceph_quarantine_revalidate(inode);
 		if (ret)
 			return ret;
 		return fscrypt_ioctl_get_policy(file, (void __user *)arg);
 
 	case FS_IOC_GET_ENCRYPTION_POLICY_EX:
 		ret = vet_mds_for_fscrypt(file);
+		if (ret)
+			return ret;
+		ret = ceph_quarantine_revalidate(inode);
 		if (ret)
 			return ret;
 		return fscrypt_ioctl_get_policy_ex(file, (void __user *)arg);
@@ -429,6 +439,9 @@ long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 
 	case FS_IOC_GET_ENCRYPTION_NONCE:
 		ret = vet_mds_for_fscrypt(file);
+		if (ret)
+			return ret;
+		ret = ceph_quarantine_revalidate(inode);
 		if (ret)
 			return ret;
 		return fscrypt_ioctl_get_nonce(file, (void __user *)arg);
fs/ceph/mds_client.c +5 -0
index bb7cf9384fd6..dcac8a6a8657 100644
@@ -4667,6 +4667,8 @@ static void handle_session(struct ceph_mds_session *session,
 		mdsc->s_cap_auths = cap_auths;
 
 		session->s_features = features;
+		if (test_bit(CEPHFS_FEATURE_QUARANTINE, &session->s_features))
+			ceph_quarantine_enable(mdsc);
 		if (test_bit(CEPHFS_FEATURE_METRIC_COLLECT,
 			     &session->s_features))
 			ceph_metric_bind_session(mdsc, session);
@@ -6482,6 +6484,9 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc)
 	spin_lock_init(&mdsc->stopping_lock);
 	atomic_set(&mdsc->stopping_blockers, 0);
 	atomic_set(&mdsc->quarantine_count, 0);
+	mdsc->quarantine_supported = false;
+	init_waitqueue_head(&mdsc->quarantine_waitq);
+	INIT_LIST_HEAD(&mdsc->quarantine_splice_owners);
 	atomic_set(&mdsc->quarantine_writers, 0);
 	atomic_set(&mdsc->quarantine_splices, 0);
 	atomic64_set(&mdsc->quarantine_epoch, 0);
fs/ceph/mds_client.h +13 -1
index 816f5f2d32cc..422a27e44c38 100644
@@ -39,7 +39,14 @@ enum ceph_feature_type {
 	CEPHFS_FEATURE_MDS_AUTH_CAPS_CHECK,
 	CEPHFS_FEATURE_SUBVOLUME_METRICS,
 
-	CEPHFS_FEATURE_MAX = CEPHFS_FEATURE_SUBVOLUME_METRICS,
+	/*
+	 * Bit 23 is CEPHFS_FEATURE_BLOCKDIFF on the MDS and is not
+	 * advertised here. Bit 24 is subvolume quarantine; it must stay
+	 * aligned with the userspace client.
+	 */
+	CEPHFS_FEATURE_QUARANTINE = 24,
+
+	CEPHFS_FEATURE_MAX = CEPHFS_FEATURE_QUARANTINE,
 };
 
 #define CEPHFS_FEATURES_CLIENT_SUPPORTED {	\
@@ -57,6 +64,7 @@ enum ceph_feature_type {
 	CEPHFS_FEATURE_HAS_OWNER_UIDGID,	\
 	CEPHFS_FEATURE_MDS_AUTH_CAPS_CHECK,	\
 	CEPHFS_FEATURE_SUBVOLUME_METRICS,	\
+	CEPHFS_FEATURE_QUARANTINE,		\
 }
 
 /*
@@ -507,6 +515,10 @@ struct ceph_mds_client {
 	spinlock_t              stopping_lock;  /* protect snap_empty */
 	int                     stopping;      /* the stage of shutting down */
 	atomic_t                stopping_blockers;
+	/* Once negotiated, keep tracking across reconnects and mixed ranks. */
+	bool			quarantine_supported;
+	wait_queue_head_t	quarantine_waitq;
+	struct list_head	quarantine_splice_owners; /* quarantine_lock */
 	/*
 	 * Inodes with CEPH_I_QUARANTINE set. Hot paths skip the
 	 * ancestor walk while this is zero.
fs/ceph/super.c +4 -2
index ad9460d9673c..ed2eab9d6969 100644
@@ -1137,11 +1137,13 @@ static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
 	err = ceph_mdsc_do_request(mdsc, NULL, req);
 	if (err == 0) {
 		struct inode *inode = req->r_target_inode;
+		struct ceph_quarantine_splice qsplice;
+
 		req->r_target_inode = NULL;
 		doutc(cl, "success\n");
-		ceph_quarantine_splice_start(fsc->sb);
+		ceph_quarantine_splice_start(fsc->sb, &qsplice);
 		root = d_make_root(inode);
-		ceph_quarantine_splice_finish(fsc->sb);
+		ceph_quarantine_splice_finish(&qsplice);
 		if (!root) {
 			root = ERR_PTR(-ENOMEM);
 			goto out;
fs/ceph/super.h +33 -9

87 lines not on this page · raw message

fs/ceph/xattr.c +52 -2

90 lines not on this page · raw message

#6 lore raw

Re: [PATCH v5 4/5] ceph: record subvolume quarantine from MDS cap updates

In reply to Alex Markuze
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] ceph: epoch self-invalidation in ceph_quarantine_reply_can_clear() - [High] ceph: unhashed…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] ceph: epoch self-invalidation in ceph_quarantine_reply_can_clear()
- [High] ceph: unhashed directory alias traversal in ceph_quarantine_walk_once()
- [High] ceph: pre-import cap sequence off-by-one in handle_cap_grant()
- [Medium] ceph: unconditional quarantine reset on non-reporting MDS sessions --

commit 78e79694b7e226fb4224da6b24cf5abda7482a66
Author: Alex Markuze <[email protected]>

ceph: record subvolume quarantine from MDS cap updates

This commit decodes the MClientCaps v14 quarantine errno to maintain a sticky, counted quarantine mark on inodes, and adds an epoch-based rcu ancestry walk cache to track whether an inode is under a quarantined subvolume.

Quoted 37 lines
fs/ceph/caps.c +5 -1
index b27bcdc24d29f..94f015112942d 100644
@@ -692,8 +692,9 @@ void change_auth_cap_ses(struct ceph_inode_info *ci,
  * @fmode is the open file mode, if we are opening a file, otherwise
  * it is < 0.  (This is so we can atomically add the cap and add an
  * open file reference to it.)
+ * Returns false when the raw grant predates the recorded cap sequence.
  */
-void ceph_add_cap(struct inode *inode,
+bool ceph_add_cap(struct inode *inode,
 		  struct ceph_mds_session *session, u64 cap_id,
 		  unsigned issued, unsigned wanted,
 		  unsigned seq, unsigned mseq, u64 realmino, int flags,
@@ -706,6 +707,7 @@ void ceph_add_cap(struct inode *inode,
 	int mds = session->s_mds;
 	int actual_wanted;
 	u32 gen;
+	bool cap_current = true;
 
 	lockdep_assert_held(&ci->i_ceph_lock);
 
@@ -737,6 +739,7 @@ void ceph_add_cap(struct inode *inode,
 		atomic64_inc(&mdsc->metric.total_caps);
 		spin_unlock(&session->s_cap_lock);
 	} else {
+		cap_current = ceph_seq_cmp(seq, cap->seq) >= 0;
 		spin_lock(&session->s_cap_lock);
 		list_move_tail(&cap->session_caps, &session->s_caps);
 		spin_unlock(&session->s_cap_lock);
@@ -823,6 +826,7 @@ void ceph_add_cap(struct inode *inode,
 	cap->mseq = mseq;
 	cap->cap_gen = gen;
 	wake_up_all(&ci->i_cap_wq);
+	return cap_current;
 }

[ ... ]

Quoted 56 lines
fs/ceph/caps.c +40 -0
@@ -3757,6 +3769,45 @@ static int parse_inline_data(void **p, void *end,
 	return -EIO;
 }
 
+/*
+ * Record quarantine state carried by an MClientCaps oserrno.
+ * Caller holds i_ceph_lock. Returns true when waiters should be woken
+ * because quarantine changed or a pending reset was resolved.
+ *
+ * -EQUARANTINED refreshes the sticky flag and its reporting MDS rank.
+ * A current grant above PIN with errno 0 clears only that rank's mark.
+ * Another rank needs a successful metadata request for the current epoch.
+ * A PIN-only update may zero qtine_errno while leaving the flag set.
+ */
+static bool ceph_apply_quarantine(struct ceph_inode_info *ci, int mds,
+				  int newcaps, int oserrno)
+{
+	bool was, now, pending;
+
+	was = READ_ONCE(ci->i_qtine_errno) == -CEPH_EQUARANTINED ||
+	      test_bit(CEPH_I_QUARANTINE_BIT, &ci->i_ceph_flags);
+	pending = test_bit(CEPH_I_QUARANTINE_PENDING_BIT, &ci->i_ceph_flags);
+
+	if ((newcaps & ~CEPH_CAP_PIN) && was && oserrno == 0 &&
+	    READ_ONCE(ci->i_qtine_mds) == mds)
+		ceph_quarantine_clear(ci);
+
+	if (oserrno == -CEPH_EQUARANTINED) {
+		ceph_quarantine_set(ci);
+		WRITE_ONCE(ci->i_qtine_mds, mds);
+	}
+
+	if (newcaps || was ||
+	    test_bit(CEPH_I_QUARANTINE_BIT, &ci->i_ceph_flags))
+		WRITE_ONCE(ci->i_qtine_errno, oserrno);
+
+	now = READ_ONCE(ci->i_qtine_errno) == -CEPH_EQUARANTINED ||
+	      test_bit(CEPH_I_QUARANTINE_BIT, &ci->i_ceph_flags);
+	return was != now ||
+	       pending != test_bit(CEPH_I_QUARANTINE_PENDING_BIT,
+				   &ci->i_ceph_flags);
+}
+
 /*
  * Handle a cap GRANT message from the MDS.  (Note that a GRANT may
  * actually be a revocation if it specifies a smaller cap set.)
@@ -3781,6 +3832,7 @@ static void handle_cap_grant(struct inode *inode,
 	u64 max_size = le64_to_cpu(grant->max_size);
 	unsigned char check_caps = 0;
 	bool was_stale = cap->cap_gen < atomic_read(&session->s_cap_gen);
+	bool quarantine_current = ceph_seq_cmp(seq, cap->seq) >= 0;
 	bool wake = false;
 	bool writeback = false;
 	bool queue_trunc = false;
@@ -4128,6 +4180,15 @@ static void handle_cap_grant(struct inode *inode,
 			fill_inline = true;

fs/ceph/caps.c static void handle_cap_grant(struct inode *inode, · line 4190

Quoted 10 lines
fs/ceph/caps.c +8 -0
 	}
 
+	if (quarantine_current && extra_info->oserrno_valid &&
+	    ceph_apply_quarantine(ci, session->s_mds, newcaps,
+				  extra_info->oserrno)) {
+		boutc(cl, "quarantine oserrno %d caps %s %llx.%llx\n",
+		      extra_info->oserrno, ceph_cap_string(newcaps),
+		      ceph_vinop(inode));
+		wake = true;
+	}

[Severity: High]
When an export target mds sends a cap message or reply just before cap import, can ceph_seq_cmp(seq, cap->seq) >= 0 treat a pre-import sequence number as current?

In fs/ceph/caps.c, when an export message arrives before the corresponding import message, handle_cap_export() sets the target mds cap sequence to t_issue_seq - 1 and merges the exported caps into cap->issued:

fs/ceph/caps.c:handle_cap_export() {
	if (tcap->cap_id == t_cap_id &&
	    ceph_seq_cmp(tcap->seq, t_issue_seq) < 0) {
		tcap->cap_id = t_cap_id;
		tcap->seq = t_issue_seq - 1;
		tcap->issue_seq = t_issue_seq - 1;
		tcap->issued |= issued;
		...
	}
	...
	} else if (tsession) {
		ceph_add_cap(inode, tsession, t_cap_id, issued, 0,
			     t_issue_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
	}
}

Both ceph_add_cap() and handle_cap_grant() treat any message with ceph_seq_cmp(seq, cap->seq) <= 0 (including seq == cap->seq, which is t_issue_seq - 1) as a message sent before the cap import, and merge cap->issued into issued or newcaps:

fs/ceph/caps.c:handle_cap_grant() {
	if (ceph_seq_cmp(seq, cap->seq) <= 0) {
		WARN_ON(cap != ci->i_auth_cap);
		WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
		seq = cap->seq;
		newcaps |= cap->issued;
	}
}

Because cap_current in ceph_add_cap() and quarantine_current in handle_cap_grant() use ceph_seq_cmp(seq, cap->seq) >= 0, a pre-import message with seq == t_issue_seq - 1 satisfies both <= 0 and >= 0. In handle_cap_grant(), newcaps is inflated with cap->issued from the export placeholder before ceph_apply_quarantine() checks (newcaps & ~CEPH_CAP_PIN), and in ceph_add_cap(), cap_current returns true to ceph_fill_inode() for a pre-import reply.

Should pre-import messages with seq == cap->seq (when the op is not CEPH_CAP_OP_IMPORT) be excluded from updating or clearing quarantine state?

[ ... ]

Quoted 4 lines
fs/ceph/inode.c +0 -0
index 94f5215f8eed5..78e97cf2f9a41 100644

[ ... ]

Quoted 44 lines
fs/ceph/inode.c +44 -0
+/*
+ * Parent walk with no inode references. Caller holds rcu_read_lock().
+ * Stops at the mount root, at an MDS mark, or at an ancestor whose
+ * cached answer is for this epoch. No hashed alias, or the depth
+ * limit, is not an answer: the caller must not cache it.
+ */
+static int ceph_quarantine_walk_once(struct inode *inode, u64 epoch,
+				     bool use_cache)
+{
+	struct inode *cur = inode;
+	unsigned int depth = 0;
+	bool under = false;
+	bool done = false;
+
+	while (cur && depth++ < 256) {
+		struct ceph_inode_info *ci = ceph_inode(cur);
+		struct dentry *alias;
+		struct dentry *parent;
+		struct inode *next;
+
+		if (test_bit(CEPH_I_QUARANTINE_PENDING_BIT, &ci->i_ceph_flags))
+			break;
+		if (READ_ONCE(ci->i_qtine_errno) == -CEPH_EQUARANTINED ||
+		    test_bit(CEPH_I_QUARANTINE_BIT, &ci->i_ceph_flags)) {
+			under = true;
+			done = true;
+			break;
+		}
+
+		if (use_cache && cur != inode &&
+		    ceph_qtag_read(ci, epoch, &under)) {
+			done = true;
+			break;
+		}
+
+		alias = d_find_alias_rcu(cur);
+		if (!alias)
+			break;
+
+		parent = READ_ONCE(alias->d_parent);
+		/*
+		 * The mount root is its own parent. A disconnected
+		 * alias from d_obtain_alias(), or a secondary root from
+		 * d_obtain_root(), has the same shape and is not a

fs/ceph/inode.c struct inode *ceph_alloc_inode(struct super_block *sb) · line 965

Quoted 10 lines
fs/ceph/inode.c +10 -0
+		 * finished walk.
+		 */
+		if (!parent || parent == alias) {
+			if (parent == alias && S_ISDIR(cur->i_mode) &&
+			    alias == READ_ONCE(inode->i_sb->s_root))
+				done = true;
+			break;
+		}
+
+		next = d_inode_rcu(parent);

[Severity: High]
If a directory dentry is unhashed while still referenced, could ceph_quarantine_walk_once() follow a stale d_parent chain to s_root and cache a clear result in i_qtag?

Looking at d_find_alias_rcu() in fs/dcache.c, it only checks !d_unhashed() for non-directory inodes, and returns the first alias unconditionally when S_ISDIR(inode->i_mode) is true:

fs/dcache.c:d_find_alias_rcu() {
	if (likely(!(inode_state_read(inode) & I_FREEING) && !hlist_empty(l))) {
		if (S_ISDIR(inode->i_mode)) {
			de = hlist_entry(l->first, struct dentry, d_alias);
		} else {
			hlist_for_each_entry(de, l, d_alias)
				if (!d_unhashed(de))
					break;
		}
	}
	return de;
}

If a directory dentry is unhashed via d_drop(), d_delete(), or d_invalidate() (for example, after a remote rename into a quarantined subvolume) while held open or referenced by a child dentry, alias remains on cur->i_dentry with d_unhashed(alias) true and alias->d_parent still pointing to the old parent directory.

Since ceph_quarantine_walk_once() does not check d_unhashed(alias) when parent != alias, wouldn't the walk continue up the old parent chain to s_root and publish a clear tag in ci->i_qtag?

[ ... ]

Quoted 35 lines
fs/ceph/inode.c +35 -0
+void ceph_quarantine_splice_start(struct super_block *sb)
+{
+	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(sb);
+
+	spin_lock(&mdsc->quarantine_lock);
+	atomic_inc_return(&mdsc->quarantine_splices);
+	atomic64_add_return(2, &mdsc->quarantine_epoch);
+	spin_unlock(&mdsc->quarantine_lock);
+}
+
+void ceph_quarantine_splice_finish(struct super_block *sb)
+{
+	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(sb);
+
+	spin_lock(&mdsc->quarantine_lock);
+	atomic64_add_return(2, &mdsc->quarantine_epoch);
+	atomic_dec_return(&mdsc->quarantine_splices);
+	spin_unlock(&mdsc->quarantine_lock);
+}
+
+static bool ceph_quarantine_reply_can_clear(struct ceph_inode_info *ci,
+					    struct ceph_mds_session *session)
+{
+	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->netfs.inode.i_sb);
+	struct ceph_mds_request *req;
+	u64 epoch;
+
+	if (READ_ONCE(ci->i_qtine_mds) == session->s_mds)
+		return true;
+	/* Different ranks have independent cap sequences. */
+	req = ceph_current_mds_request(mdsc->fsc);
+	epoch = (u64)atomic64_read_acquire(&mdsc->quarantine_epoch);
+	return req && !(epoch & 1) && req->r_quarantine_epoch == epoch &&
+		!atomic_read(&mdsc->quarantine_splices);
+}

[ ... ]

Quoted 10 lines
fs/ceph/inode.c +1 -1
@@ -1407,7 +1866,7 @@ int ceph_fill_inode(struct inode *inode, struct folio *locked_folio,
 	/* were we issued a capability? */
 	if (info_caps) {
 		if (!ceph_in_snap(inode)) {
-			ceph_add_cap(inode, session,
+			bool cap_current = ceph_add_cap(inode, session,
 				     le64_to_cpu(info->cap.cap_id),
 				     info_caps,
 				     le32_to_cpu(info->cap.wanted),

fs/ceph/inode.c int ceph_fill_inode(struct inode *inode, struct folio *locked_folio, · line 1884

Quoted 10 lines
fs/ceph/inode.c +7 -0
@@ -1416,6 +1875,14 @@ int ceph_fill_inode(struct inode *inode, struct folio *locked_folio,
 				     le64_to_cpu(info->cap.realm),
 				     info->cap.flags, &new_cap);
 
+			/*
+			 * Caps above PIN mean quarantine has been lifted,
+			 * including when the disable message was lost.
+			 */
+			if (cap_current && (info_caps & ~CEPH_CAP_PIN) &&
+			    ceph_quarantine_reply_can_clear(ci, session))
+				ceph_quarantine_clear(ci);

[Severity: High]
How can ceph_quarantine_reply_can_clear() succeed for req->r_target_inode or later readdir entries when earlier steps in the same reply advance mdsc->quarantine_epoch?

req->r_quarantine_epoch is sampled once in ceph_mdsc_create_request(), and ceph_quarantine_reply_can_clear() requires req->r_quarantine_epoch == epoch when session->s_mds != ci->i_qtine_mds. However, mdsc->quarantine_epoch is also incremented by ceph_quarantine_clear(), ceph_quarantine_reset(), and ceph_quarantine_splice_start()/finish():

1. In ceph_fill_trace(), ceph_fill_inode() is called on parent_dir before it is called on req->r_target_inode:

fs/ceph/inode.c:ceph_fill_trace() {
	if (parent_dir) {
		ceph_inode_set_subvolume(parent_dir,
					 rinfo->diri.subvolume_id);
		err = ceph_fill_inode(parent_dir, NULL, &rinfo->diri,
				      rinfo->dirfrag, session, -1,
				      &req->r_caps_reservation);
		...
	}
	...
	if (rinfo->head->is_target) {
		in = req->r_target_inode;
		ceph_inode_set_subvolume(in, rinfo->targeti.subvolume_id);
		err = ceph_fill_inode(in, req->r_locked_folio, &rinfo->targeti,
				...);
	}
}

If filling parent_dir clears a quarantine mark on parent_dir, ceph_quarantine_clear() advances mdsc->quarantine_epoch by 2, so req->r_quarantine_epoch == epoch becomes false before ceph_fill_inode() runs on req->r_target_inode.

2. In ceph_readdir_prepopulate(), each entry calls ceph_fill_inode() and, for negative dentries, splice_dentry(), which calls ceph_quarantine_splice_start() and ceph_quarantine_splice_finish(). On the first entry that splices a dentry or clears a quarantine mark, mdsc->quarantine_epoch advances, preventing ceph_quarantine_reply_can_clear() from clearing quarantine on any subsequent entry in the readdir reply.

3. If a request is created before the old reporting session's cap is removed or reset via __ceph_remove_cap(), remove_session_caps_cb(), or reconnect_caps_cb(), ceph_quarantine_reset() advances mdsc->quarantine_epoch after req->r_quarantine_epoch was recorded, so the reply cannot clear the pending mark on another rank.

[ ... ]

Quoted 49 lines
fs/ceph/mds_client.c +8 -1
index d4ef8a355a480..bb7cf9384fd6d 100644
@@ -2041,10 +2041,12 @@ static int remove_session_caps_cb(struct inode *inode, int mds, void *arg)
 	struct ceph_inode_info *ci = ceph_inode(inode);
 	struct ceph_client *cl = ceph_inode_to_client(inode);
 	bool invalidate = false;
+	bool reset;
 	struct ceph_cap *cap;
 	int iputs = 0;
 
 	spin_lock(&ci->i_ceph_lock);
+	reset = ceph_quarantine_reset(ci);
 	cap = __get_cap_for_mds(ci, mds);
 	if (cap) {
 		boutc(cl, " removing cap %p, ci is %p, inode is %p\n",
@@ -2054,7 +2056,7 @@ static int remove_session_caps_cb(struct inode *inode, int mds, void *arg)
 	}
 	spin_unlock(&ci->i_ceph_lock);
 
-	if (cap)
+	if (cap || reset)
 		wake_up_all(&ci->i_cap_wq);
 	if (invalidate)
 		ceph_queue_invalidate(inode);
@@ -2860,6 +2862,7 @@ ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
 
 	mutex_init(&req->r_fill_mutex);
 	req->r_mdsc = mdsc;
+	req->r_quarantine_epoch = atomic64_read_acquire(&mdsc->quarantine_epoch);
 	req->r_started = jiffies;
 	req->r_start_latency = ktime_get();
 	req->r_resend_mds = -1;
@@ -5009,6 +5012,7 @@ static int reconnect_caps_cb(struct inode *inode, int mds, void *arg)
 	struct ceph_path_info path_info = {0};
 	int err;
 	u64 snap_follows;
+	bool reset = false;
 
 	dentry = d_find_primary(inode);
 	if (dentry) {
@@ -5023,9 +5027,16 @@ static int reconnect_caps_cb(struct inode *inode, int mds, void *arg)
 	}
 
 	spin_lock(&ci->i_ceph_lock);
+	/*
+	 * Require a fresh MDS answer after reconnect. A PIN import may
+	 * carry no quarantine errno, so it must not reopen cached snap caps.

fs/ceph/mds_client.c static int reconnect_caps_cb(struct inode *inode, int mds, void *arg) · line 5042

Quoted 10 lines
fs/ceph/mds_client.c +4 -0
+	 */
+	reset = ceph_quarantine_reset(ci);
 	cap = __get_cap_for_mds(ci, mds);
 	if (!cap) {
 		spin_unlock(&ci->i_ceph_lock);
+		if (reset)
+			wake_up_all(&ci->i_cap_wq);
 		err = 0;
 		goto out_err;
 	}

[Severity: Medium]
Should remove_session_caps_cb() and reconnect_caps_cb() check whether READ_ONCE(ci->i_qtine_mds) == mds before calling ceph_quarantine_reset(ci)?

In __ceph_remove_cap(), ceph_quarantine_reset(ci) is gated on READ_ONCE(ci->i_qtine_mds) == cap->mds so that only losing the reporting rank's cap transitions the mark to pending. However, remove_session_caps_cb() and reconnect_caps_cb() call ceph_quarantine_reset(ci) unconditionally for every inode visited during a session close or reconnect, even when the quarantine mark was reported by a different mds rank whose session is still active.

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#8 lore raw

Re: [PATCH v5 5/5] ceph: fail quarantined subvolume access with EACCES

In reply to Alex Markuze
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] ceph: recursive MDS requests and deadlock in splice LSM hooks - [High] ceph: ceph_open() skips…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] ceph: recursive MDS requests and deadlock in splice LSM hooks
- [High] ceph: ceph_open() skips access and snap_caps checks on snapshots
- [High] ceph: double ceph_buffer_put() of old_blob on retry in __ceph_setxattr()
- [Medium] ceph: ceph_cap_auth_valid() skips epoch checks when quarantine_count is 0
- [Medium] ceph: missing head cap validation in xattr, ACL, and lookup paths
- [Medium] ceph: __ceph_do_getattr() misses direct-mark and post-RPC epoch checks --

commit 60ee77fdf7eb1f1cf01d2c90a0a3b53835663f69
Author: Alex Markuze <[email protected]>

ceph: fail quarantined subvolume access with EACCES

This commit advertises CEPHFS_FEATURE_QUARANTINE and gates cached data, metadata, directory, symlink, xattr, ACL, ioctl, and snapshot operations so that quarantined subvolume accesses fail with -EACCES. It also tracks namespace mutations and splices to invalidate cached quarantine receipts and prevent recursive LSM hooks from blocking on their own splice operations.

Quoted 5 lines
fs/ceph/caps.c +0 -0
index 94f015112942d..9b8afd0be194c 100644
@@ -3010,10 +3010,39 @@ enum {

[ ... ]

fs/ceph/caps.c enum { · line 3042

Quoted 11 lines
fs/ceph/caps.c +11 -0
+static bool ceph_cap_auth_valid(struct ceph_mds_client *mdsc, u64 epoch)
+{
+	if (!atomic_read(&mdsc->quarantine_count))
+		return true;
+
+	/* Order authorization and cap observations before the generation check. */
+	smp_rmb();
+	return !(epoch & 1) &&
+	       (u64)atomic64_read_acquire(&mdsc->quarantine_epoch) == epoch &&
+	       !atomic_read(&mdsc->quarantine_splices);
+}

[Severity: Medium]
Can ceph_cap_auth_valid() in fs/ceph/caps.c accept a stale or mid-mutation epoch by returning true early when mdsc->quarantine_count is zero?

In fs/ceph/inode.c:ceph_quarantine_set(), ceph_quarantine_write_begin() increments mdsc->quarantine_epoch to an odd value before atomic_inc(&mdsc->quarantine_count) transitions quarantine_count from 0 to 1:

    ceph_quarantine_write_begin(mdsc);
    if (mdsc)
        atomic_inc(&mdsc->quarantine_count);

Because the relaxed atomic_read(&mdsc->quarantine_count) in ceph_cap_auth_valid() runs before smp_rmb() and before checking !(epoch & 1), quarantine_epoch == epoch, and !atomic_read(&mdsc->quarantine_splices), a concurrent caller in __ceph_get_caps() or try_get_cap_refs() can observe quarantine_count == 0 and treat the authorization as valid while the first quarantine mark is already being set.

In addition, for snapshot inodes in __ceph_get_caps(), ceph_snapshot_needs_revalidate(inode) can be true when mdsc->quarantine_count is 0 (for example, when accessing an ancestor snapshot P/.snap/S/V/file where quarantining V leaves no local marks on P, or while a splice or rename is in progress). Returning true unconditionally when quarantine_count is 0 skips waiting in ceph_quarantine_wait() when qepoch is odd or quarantine_splices is non-zero, and skips detecting epoch changes in try_get_cap_refs().

Quoted 5 lines
fs/ceph/file.c +0 -0
index 9a6eadb94ab4f..5b3a64c8571c9 100644
@@ -371,7 +373,9 @@ int ceph_open(struct inode *inode, struct file *file)

[ ... ]

Quoted 62 lines
fs/ceph/file.c +26 -20
@@ -414,28 +435,31 @@ int ceph_open(struct inode *inode, struct file *file)
 
 	if (fmode & CEPH_FILE_MODE_WR)
 		mask |= MAY_WRITE;
-	dentry = d_find_alias(inode);
-	if (!dentry) {
-		do_sync = true;
-	} else {
-		struct ceph_path_info path_info = {0};
-		path = ceph_mdsc_build_path(mdsc, dentry, &path_info, 0);
-		if (IS_ERR(path)) {
+	if (!snapshot) {
+		dentry = d_find_alias(inode);
+		if (!dentry) {
 			do_sync = true;
-			err = 0;
 		} else {
-			err = ceph_mds_check_access(mdsc, path, mask);
-		}
-		ceph_mdsc_free_path_info(&path_info);
-		dput(dentry);
+			struct ceph_path_info path_info = {0};
 
-		/* For none EACCES cases will let the MDS do the mds auth check */
-		if (err == -EACCES) {
-			ceph_blog_exit(&__ji);
-			return err;
-		} else if (err < 0) {
-			do_sync = true;
-			err = 0;
+			path = ceph_mdsc_build_path(mdsc, dentry, &path_info, 0);
+			if (IS_ERR(path)) {
+				do_sync = true;
+				err = 0;
+			} else {
+				err = ceph_mds_check_access(mdsc, path, mask);
+			}
+			ceph_mdsc_free_path_info(&path_info);
+			dput(dentry);
+
+			/* Let the MDS handle errors other than EACCES. */
+			if (err == -EACCES) {
+				ceph_blog_exit(&__ji);
+				return err;
+			} else if (err < 0) {
+				do_sync = true;
+				err = 0;
+			}
 		}
 	}
 
@@ -445,12 +469,23 @@ int ceph_open(struct inode *inode, struct file *file)
 		return -EROFS;
 	}
 
-	/* trivially open snapdir */
+	/* A synthetic snapdir must be validated through its real parent. */
 	if (ceph_snap(inode) == CEPH_SNAPDIR) {
+		err = ceph_quarantine_revalidate(inode);
 		ceph_blog_exit(&__ji);
+		if (err)
+			return err;

fs/ceph/file.c int ceph_open(struct inode *inode, struct file *file) · line 487

Quoted 10 lines
fs/ceph/file.c +7 -0
 		return ceph_init_file(inode, file, fmode);
 	}
 
+	if (snapshot) {
+		err = ceph_snapshot_read_auth(inode);
+		if (!err)
+			err = ceph_init_file(inode, file, fmode);
+		ceph_blog_exit(&__ji);
+		return err;
+	}

[Severity: High]
Does gating ceph_mds_check_access() on !snapshot and returning early from ceph_open() in fs/ceph/file.c bypass MDS auth cap checks and i_snap_caps verification when opening cached snapshot files?

In fs/ceph/inode.c:ceph_snapshot_read_auth(), the function returns 0 without contacting the MDS whenever !ceph_snapshot_needs_revalidate(inode) holds:

    int ceph_snapshot_read_auth(struct inode *inode)
    {
        if (!ceph_snapshot_needs_revalidate(inode))
            return 0;

        return ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, true);
    }

That condition is satisfied whenever mdsc->quarantine_supported is false, as well as when quarantine_supported is true, mdsc->quarantine_count is 0, and the connected subvolume root head inode holds a valid CEPH_CAP_AUTH_SHARED cap.

When ceph_snapshot_read_auth() returns 0 from cache, ceph_open() now skips both the local ceph_mds_check_access(mdsc, path, mask) check against mdsc->s_cap_auths and any MDS OPEN or GETATTR request, allowing a caller restricted by UID, GID, or path in s_cap_auths to open cached snapshot inodes without getting -EACCES.

Returning unconditionally in the if (snapshot) block also skips checking whether (ci->i_snap_caps & wanted) == wanted (and ceph_snapshot_read_auth() only requests CEPH_CAP_AUTH_SHARED rather than wanted when it does call the MDS). This allows a snapshot inode that only holds CEPH_CAP_PIN (such as one populated via LOOKUPINO) to be opened without CEPH_CAP_FILE_RD, and leaves the existing snapshot cap check further down in ceph_open() unreachable:

    } else if (!do_sync && ceph_in_snap(inode) &&
               (ci->i_snap_caps & wanted) == wanted) {
Quoted 28 lines
fs/ceph/inode.c +13 -1
index 78e97cf2f9a41..781b6d97daa0e 100644
@@ -3582,21 +3958,59 @@ int __ceph_do_getattr(struct inode *inode, struct folio *locked_folio,
 	struct ceph_client *cl = fsc->client;
 	struct ceph_mds_client *mdsc = fsc->mdsc;
 	struct ceph_mds_request *req;
+	bool head_revalidate;
+	u64 qepoch;
 	int mode;
 	int err;
 
 	if (ceph_snap(inode) == CEPH_SNAPDIR) {
+		struct inode *parent;
+
 		boutc(cl, "inode %p %llx.%llx SNAPDIR\n", inode,
 		      ceph_vinop(inode));
-		return 0;
+		if (!ceph_snapshot_needs_revalidate(inode))
+			return 0;
+		/* The synthetic snapdir has the real directory's ino. */
+		parent = ceph_lookup_inode(inode->i_sb, ceph_ino(inode));
+		if (IS_ERR(parent))
+			return PTR_ERR(parent);
+		err = ceph_do_getattr(parent, mask, true);
+		iput(parent);
+		return err;

fs/ceph/inode.c int __ceph_do_getattr(struct inode *inode, struct folio *locked_folio, · line 3989

Quoted 10 lines
fs/ceph/inode.c +8 -0
 	}
 
+	head_revalidate = ceph_quarantine_head_needs_revalidate(inode);
+	if (head_revalidate) {
+		/* Do not recurse into an MDS request from our own splice hook. */
+		err = ceph_quarantine_wait(inode);
+		if (err)
+			return err;
+		mask |= CEPH_CAP_AUTH_SHARED;
+	}

[Severity: High]
What happens when an LSM hook invoked from d_splice_alias(), d_instantiate(), or d_make_root() re-enters __ceph_getxattr(), ceph_listxattr(), or __ceph_do_getattr() while ceph_fill_inode() has already granted CEPH_CAP_AUTH_SHARED or when splicing a snapshot or snapdir inode?

During d_splice_alias(), d_instantiate(), and d_make_root(), the VFS runs security_d_instantiate(dentry, inode) before linking the dentry onto inode->i_dentry. At the four splice sites outside ceph_fill_trace() -- ceph_finish_async_create() in fs/ceph/file.c, ceph_handle_snapdir() and ceph_link() in fs/ceph/dir.c, and open_root_dentry() in fs/ceph/super.c -- ceph_current_fill_trace_request() is NULL while qsplice is active.

When selinux_d_instantiate() calls __vfs_getxattr() -> __ceph_getxattr() (or ceph_listxattr()) in fs/ceph/xattr.c:

    if (!ceph_current_fill_trace_request()) {
        spin_unlock(&ci->i_ceph_lock);
        ask_mds = ceph_snapshot_needs_revalidate(inode) ||
                  ceph_inode_quarantine_state(inode) != CEPH_Q_CLEAR;
        spin_lock(&ci->i_ceph_lock);
    }

because the dentry is not yet attached to the inode (d_find_alias_rcu(inode) is NULL) and mdsc->quarantine_splices is non-zero, ceph_inode_quarantine_state(inode) returns CEPH_Q_UNKNOWN whenever mdsc->quarantine_count > 0, and ceph_snapshot_needs_revalidate(inode) returns true for CEPH_SNAPDIR inodes even when mdsc->quarantine_count == 0. That sets ask_mds = true, bypassing the xattr blob just installed by ceph_fill_inode() and calling ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true).

In __ceph_do_getattr(), ceph_quarantine_wait(inode) -- the only helper that checks mdsc->quarantine_splice_owners and ceph_current_fill_trace_request() -- is only called when head_revalidate is true. However, ceph_fill_inode() has already granted CEPH_CAP_AUTH_SHARED on ci->i_auth_cap in ceph_finish_async_create(), ceph_fill_trace(), ceph_readdir_prepopulate(), open_root_dentry(), and ceph_link(), and head_revalidate is always false for snapshot and snapdir inodes. As a result, __ceph_do_getattr() skips ceph_quarantine_wait(inode) and issues a synchronous CEPH_MDS_OP_GETATTR request from inside the splice LSM hook:

1. In ceph_finish_async_create(), CEPH_I_ASYNC_CREATE_BIT and CEPH_DENTRY_ASYNC_CREATE_BIT are set before d_splice_alias(); the recursive GETATTR blocks in ceph_wait_on_async_create(inode) while d_inode(dentry) is still NULL, deadlocking if the async create request fails or is forwarded.
2. In ceph_fill_trace() and ceph_readdir_prepopulate(), an LSM hook calling ceph_getattr(), ceph_listxattr(), ceph_permission(), or ceph_get_link() on an inode with AUTH_SHARED or a snapshot inode deadlocks the MDS session thread on session->s_mutex.
3. In ceph_handle_snapdir(), __ceph_do_getattr() on CEPH_SNAPDIR calls ceph_do_getattr(parent, mask, true), either failing with -EBUSY and breaking SELinux snapdir labeling or sending a synchronous GETATTR while holding qsplice.

Quoted 31 lines
fs/ceph/inode.c +18 -1
+
+	/*
+	 * Cached caps would satisfy getattr after the MDS has revoked
+	 * access, and a short walk has no ancestor to consult. Ask the
+	 * MDS so a lost quarantine-disable can clear the mark, and so
+	 * an uncached parent chain is not treated as clear. Snapshot caps need
+	 * an MDS check unless a live head cap provides quarantine notifications.
+	 */
+	if (ceph_snapshot_needs_revalidate(inode) ||
+	    head_revalidate ||
+	    ceph_inode_quarantine_state(inode) != CEPH_Q_CLEAR)
+		force = true;
 
 	boutc(cl, "inode %p %llx.%llx mask %s mode 0%o\n", inode,
 	      ceph_vinop(inode), ceph_cap_string(mask), inode->i_mode);
 	if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1))
 			return 0;
 
-	mode = ceph_try_to_choose_auth_mds(inode, mask);
+	if (ceph_in_snap(inode) && !ceph_snapshot_has_path(inode, false))
+		return ceph_snapshot_lookupino(inode, locked_folio, mask);
+
+	qepoch = (u64)atomic64_read_acquire(&mdsc->quarantine_epoch);
+	mode = head_revalidate ? USE_AUTH_MDS :
+		ceph_try_to_choose_auth_mds(inode, mask);
 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
 	if (IS_ERR(req))
 		return PTR_ERR(req);
@@ -3606,6 +4020,15 @@ int __ceph_do_getattr(struct inode *inode, struct folio *locked_folio,
 	req->r_args.getattr.mask = cpu_to_le32(mask);
 	req->r_locked_folio = locked_folio;

fs/ceph/inode.c int __ceph_do_getattr(struct inode *inode, struct folio *locked_folio, · line 4031

Quoted 10 lines
fs/ceph/inode.c +9 -0
 	err = ceph_mdsc_do_request(mdsc, NULL, req);
+	if (ceph_in_snap(inode)) {
+		if (err == -EINVAL || err == -ESTALE ||
+		    (!err && ceph_snapshot_reply_error(inode, req, mask))) {
+			ceph_mdsc_put_request(req);
+			return ceph_snapshot_lookupino(inode, locked_folio, mask);
+		}
+	}
+	if (!err && !ceph_in_snap(inode))
+		ceph_quarantine_remember_clear(inode, qepoch, req->r_cred);

[Severity: Medium]
Should __ceph_do_getattr() in fs/ceph/inode.c also check ceph_inode_quarantine_direct(inode) and retry when quarantine_epoch changes across the GETATTR request, similar to ceph_quarantine_revalidate()?

Callers that rely on ceph_do_getattr() to validate quarantine access -- including ceph_getattr(), __ceph_getxattr(), ceph_listxattr(), ceph_get_link(), ceph_encrypted_get_link(), ceph_snapshot_read_auth(), ceph_permission(), and ceph_ioctl_get_dataloc() -- do not call ceph_quarantine_revalidate().

Unlike ceph_quarantine_revalidate(), __ceph_do_getattr() does not check ceph_inode_quarantine_direct(inode) before or after ceph_mdsc_do_request(). When head_revalidate is false (for example, the inode already holds CEPH_CAP_AUTH_SHARED while an ancestor is marked or uncached) and force is set because ceph_inode_quarantine_state(inode) != CEPH_Q_CLEAR, __ceph_do_getattr() does not wait in ceph_quarantine_wait(inode) before sampling qepoch and uses ceph_try_to_choose_auth_mds(inode, mask), which can send the request to a replica MDS (USE_ANY_MDS).

Furthermore, after ceph_mdsc_do_request() returns 0, ceph_quarantine_remember_clear(inode, qepoch, req->r_cred) drops the receipt if quarantine_epoch changed, a splice is active, or ceph_inode_quarantine_direct(inode) became true during the RPC, but __ceph_do_getattr() still returns 0 to the caller.

Quoted 5 lines
fs/ceph/xattr.c +0 -0
index 4cba6e7bffd3d..9db250b2e7beb 100644
@@ -997,6 +997,7 @@ ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,

[ ... ]

Quoted 36 lines
fs/ceph/xattr.c +16 -2
@@ -1034,7 +1035,19 @@ ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
 	      ceph_vinop(inode), name, ci->i_xattrs.version,
 	      ci->i_xattrs.index_version);
 
-	if (ci->i_xattrs.version == 0 ||
+	/*
+	 * A shared cap would answer from the cache. The state check may
+	 * schedule, so drop i_ceph_lock first. A reply reading the blob
+	 * it just installed must still use that cache.
+	 */
+	if (!ceph_current_fill_trace_request()) {
+		spin_unlock(&ci->i_ceph_lock);
+		ask_mds = ceph_snapshot_needs_revalidate(inode) ||
+			  ceph_inode_quarantine_state(inode) != CEPH_Q_CLEAR;
+		spin_lock(&ci->i_ceph_lock);
+	}
+
+	if (ask_mds || ci->i_xattrs.version == 0 ||
 	    !((req_mask & CEPH_CAP_XATTR_SHARED) ||
 	      __ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1))) {
 		spin_unlock(&ci->i_ceph_lock);
@@ -1089,6 +1102,7 @@ ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
 	struct ceph_client *cl = ceph_inode_to_client(inode);
 	struct ceph_inode_info *ci = ceph_inode(inode);
 	bool len_only = (size == 0);
+	bool ask_mds = false;
 	u32 namelen;
 	int err;
 	struct ceph_journal_info __ji;
@@ -1100,7 +1114,15 @@ ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
 	      ceph_vinop(inode), ci->i_xattrs.version,
 	      ci->i_xattrs.index_version);
 
-	if (ci->i_xattrs.version == 0 ||
+	/* Same as __ceph_getxattr(): do not serve a shared cap hit. */
+	if (!ceph_current_fill_trace_request()) {

fs/ceph/xattr.c ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size) · line 1128

Quoted 10 lines
fs/ceph/xattr.c +7 -0
+		spin_unlock(&ci->i_ceph_lock);
+		ask_mds = ceph_snapshot_needs_revalidate(inode) ||
+			  ceph_inode_quarantine_state(inode) != CEPH_Q_CLEAR;
+		spin_lock(&ci->i_ceph_lock);
+	}
+
+	if (ask_mds || ci->i_xattrs.version == 0 ||
 	    !__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1)) {
 		spin_unlock(&ci->i_ceph_lock);
 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);

[Severity: Medium]
Should __ceph_getxattr() and ceph_listxattr() in fs/ceph/xattr.c (as well as ceph_lookup() in fs/ceph/dir.c) also check ceph_quarantine_head_needs_revalidate()?

In fs/ceph/dir.c:ceph_lookup(), negative complete-dir lookups only check ceph_inode_quarantine_state(dir):

    if (d_really_is_negative(dentry) &&
        ceph_inode_quarantine_state(dir) == CEPH_Q_CLEAR) {

When mdsc->quarantine_count is 0, ceph_inode_quarantine_state() returns CEPH_Q_CLEAR immediately. Because __ceph_caps_issued_mask_metric() checks whether CEPH_CAP_XATTR_SHARED or CEPH_CAP_FILE_SHARED is issued on any cap in ci->i_caps (including replica MDS caps, sessions without CEPHFS_FEATURE_QUARANTINE, or when ci->i_auth_cap lacks CEPH_CAP_AUTH_SHARED), __ceph_getxattr(), ceph_get_acl(), ceph_listxattr(), and ceph_lookup() can serve cached xattrs, POSIX ACLs, and negative lookups without a quarantine- aware authoritative AUTH_SHARED cap.

Similarly, when mdsc->quarantine_count > 0, __ceph_do_getattr() records a read receipt via ceph_quarantine_remember_clear() even when no cap was granted by the MDS. Subsequent calls to __ceph_getxattr(), ceph_get_acl(), ceph_listxattr(), and ceph_lookup() in the same epoch see ceph_inode_quarantine_state() == CEPH_Q_CLEAR and reuse that receipt from cache without a revocable cap.

Quoted 28 lines
fs/ceph/xattr.c +17 -0
@@ -1222,6 +1244,7 @@ int __ceph_setxattr(struct inode *inode, const char *name,
 	int required_blob_size;
 	bool check_realm = false;
 	bool lock_snap_rwsem = false;
+	bool checked_quarantine = false;
 
 	if (ceph_in_snap(inode))
 		return -EROFS;
@@ -1319,6 +1342,33 @@ int __ceph_setxattr(struct inode *inode, const char *name,
 		goto retry;
 	}
 
+	/*
+	 * XATTR_EXCL applies the new blob with no MDS round trip.
+	 * The state check may schedule, so drop i_ceph_lock first.
+	 */
+	if (!checked_quarantine) {
+		enum ceph_qstate qstate;
+
+		spin_unlock(&ci->i_ceph_lock);
+		qstate = ceph_inode_quarantine_write_state(inode);
+		if (qstate == CEPH_Q_UNDER &&
+		    ceph_inode_quarantine_direct(inode)) {
+			ceph_buffer_put(old_blob);
+			if (lock_snap_rwsem)
+				up_read(&mdsc->snap_rwsem);
+			err = -EACCES;
+			goto out;

fs/ceph/xattr.c int __ceph_setxattr(struct inode *inode, const char *name, · line 1370

Quoted 10 lines
fs/ceph/xattr.c +10 -0
+		}
+		if (qstate != CEPH_Q_CLEAR) {
+			ceph_buffer_put(old_blob);
+			old_blob = NULL;
+			goto do_sync_unlocked;
+		}
+		checked_quarantine = true;
+		spin_lock(&ci->i_ceph_lock);
+		goto retry;
+	}

[Severity: High]
Can jumping to retry from the !checked_quarantine block in fs/ceph/xattr.c:__ceph_setxattr() without releasing and clearing old_blob cause a double ceph_buffer_put(old_blob)?

Just above this block in __ceph_setxattr(), when a prealloc_blob needs to be allocated or grown, the code saves any previous ci->i_xattrs.prealloc_blob in old_blob and jumps to retry without resetting old_blob = NULL after ceph_buffer_put(old_blob):

    if (!ci->i_xattrs.prealloc_blob ||
        required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
        struct ceph_buffer *blob;

        spin_unlock(&ci->i_ceph_lock);
        ceph_buffer_put(old_blob); /* Shouldn't be required */
        ...
        blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
        if (!blob)
            goto do_sync_unlocked;
        spin_lock(&ci->i_ceph_lock);
        /* prealloc_blob can't be released while holding i_ceph_lock */
        if (ci->i_xattrs.prealloc_blob)
            old_blob = ci->i_xattrs.prealloc_blob;
        ci->i_xattrs.prealloc_blob = blob;
        goto retry;
    }

If the first pass replaces an existing smaller ci->i_xattrs.prealloc_blob, execution reaches the new !checked_quarantine block on the second pass with old_blob != NULL. That block drops ci->i_ceph_lock to call ceph_inode_quarantine_write_state(inode), and when qstate == CEPH_Q_CLEAR it sets checked_quarantine = true, reacquires ci->i_ceph_lock, and jumps to retry still holding the non-NULL old_blob.

If a concurrent cap flush or cap snap (__prep_cap() or ceph_queue_cap_snap() calling __ceph_build_xattrs_blob()) runs while ci->i_ceph_lock is dropped and consumes ci->i_xattrs.prealloc_blob (setting ci->i_xattrs.prealloc_blob = NULL), the next pass of retry will enter the prealloc_blob allocation block again. That block calls ceph_buffer_put(old_blob) to free old_blob, sees ci->i_xattrs.prealloc_blob == NULL so it leaves old_blob pointing to the freed buffer, and then the final ceph_buffer_put(old_blob) after __set_xattr() (or at do_sync) frees old_blob a second time.

Quoted 3 lines
fs/ceph/xattr.c +0 -0
 	err = __set_xattr(ci, newname, name_len, newval, val_len,
 			  flags, value ? 1 : -1, &xattr);

[ ... ]

Quoted 3 lines
Signature · 2 lines
-- 
2.51.0
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
maintainer
#9 lore raw

Re: [PATCH v5 5/5] ceph: fail quarantined subvolume access with EACCES

In reply to Alex Markuze
Quoted 10 lines · Alex Markuze
fs/ceph/inode.c +5 -0
+/*
+ * fscrypt can cache the decrypted target in ->i_link, allowing the VFS to
+ * bypass this callback. Checking those cache hits needs separate fscrypt
+ * support; this check covers only accesses that reach ->get_link().
+ */
 static const char *ceph_encrypted_get_link(struct dentry *dentry,
 					   struct inode *inode,
 					   struct delayed_call *done)
 {
 	struct ceph_inode_info *ci = ceph_inode(inode);

fs/ceph/inode.c static void ceph_inode_work(struct work_struct *work) · line 3172

Quoted 10 lines
fs/ceph/inode.c +4 -0
+	int err;
 
 	if (!dentry)
 		return ERR_PTR(-ECHILD);
+	err = ceph_symlink_revalidate(inode);
+	if (err)
+		return ERR_PTR(err);
 
 	return fscrypt_get_symlink(inode, ci->i_symlink, i_size_read(inode),
 				   done);

The function comment seems out of sync with the actual code, as the actual code disables the symlink target caching.

Quoted 18 lines
fs/ceph/ioctl.c +6 -0
 	case FS_IOC_GET_ENCRYPTION_POLICY:
 		ret = vet_mds_for_fscrypt(file);
+		if (ret)
+			return ret;
+		ret = ceph_quarantine_revalidate(inode);
 		if (ret)
 			return ret;
 		return fscrypt_ioctl_get_policy(file, (void __user *)arg);
 
 	case FS_IOC_GET_ENCRYPTION_POLICY_EX:
 		ret = vet_mds_for_fscrypt(file);
+		if (ret)
+			return ret;
+		ret = ceph_quarantine_revalidate(inode);
 		if (ret)
 			return ret;
 		return fscrypt_ioctl_get_policy_ex(file, (void __user *)arg);

fs/ceph/ioctl.c long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg) · line 447

Quoted 9 lines
fs/ceph/ioctl.c +3 -0
@@ -429,6 +439,9 @@ long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 
 	case FS_IOC_GET_ENCRYPTION_NONCE:
 		ret = vet_mds_for_fscrypt(file);
+		if (ret)
+			return ret;
+		ret = ceph_quarantine_revalidate(inode);
 		if (ret)
 			return ret;
 		return fscrypt_ioctl_get_nonce(file, (void __user *)arg);

Can you clarify why FS_IOC_SET_ENCRYPTION_POLICY doesn't need this too?

- Eric

maintainer
#2 lore raw

Re: [PATCH v5 1/5] dcache: export d_find_alias_rcu() for modular filesystems

In reply to Alex Markuze
Quoted 11 lines · Alex Markuze

The Ceph subvolume quarantine ancestry walk needs to find a dentry alias under RCU without taking a reference. d_find_alias_rcu() provides these semantics, but it is not exported, so a modular Ceph client fails at modpost with an undefined reference to the helper.

Export d_find_alias_rcu() to GPL modules so filesystem modules can use the existing RCU lookup without changing its lifetime rules.

This is a prerequisite for later commits rather than a bug fix. So it shouldn't really have Reported-by and Closes tags, even if you found out it was needed via the kernel test robot.

- Eric