Skip to contentSkip to conversation

NFS: defer access-cache entry and group_info free past RCU

PATCH New Cheng Yongkang 1 participant 1 message Nov 30

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 NFS: defer access-cache entry and group_info free past RCU

nfs_access_free_entry() drops the group_info reference synchronously, while the nfs_access_entry itself is only freed after an RCU grace period. nfs_access_get_cached_rcu() calls access_cmp() on the most recently used entry under rcu_read_lock(), and access_cmp() reads the entry's group_info->ngroups and gid[] whenever the caller's group_info is a different object. An entry that is replaced in nfs_access_add_rbtree(), or removed by the shrinker or nfs_access_zap_cache(), can still be in use by such a reader. If the entry holds the last reference, put_group_info() frees the array while the reader is comparing it:

 BUG: KASAN: use-after-free in nfs_access_get_cached+0xb8f/0xf80
 Read of size 4 at addr ... by task poc/9503
 Call Trace:
  nfs_access_get_cached
  nfs_do_access
  nfs_permission
  inode_permission
  do_faccessat
 freed by:
  nfs_access_free_entry
  nfs_access_add_cache
  nfs_do_access

Before commit 6238aec83f3f ("NFS: don't store 'struct cred *' in struct nfs_access_entry"), the entry held a cred, and put_cred() freed the cred, and with it the group_info, only after a grace period.

Drop the group_info reference in an RCU callback that then frees the entry. put_group_info() can run there: groups_free() uses kvfree(), which may be called in interrupt context, and put_cred_rcu() already drops a group_info reference from an RCU callback.

Fixes 6238aec83f3f ("NFS: don't store 'struct cred *' in struct nfs_access_entry")

---
Changes in v3:
- Use call_rcu() instead of queue_rcu_work(); put_group_info() can run in an RCU callback (Trond).
- Leave struct nfs_access_entry unchanged.
- Describe the KASAN report as a use-after-free, not a slab use-after-free.

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

 fs/nfs/dir.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
fs/nfs/dir.c +11 -2
index 49437037f948..deed0dc49fc2 100644
@@ -2895,10 +2895,19 @@ static unsigned long nfs_access_max_cachesize = 4*1024*1024;
 module_param(nfs_access_max_cachesize, ulong, 0644);
 MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
 
-static void nfs_access_free_entry(struct nfs_access_entry *entry)
+static void nfs_access_free_entry_rcu(struct rcu_head *head)
 {
+	struct nfs_access_entry *entry =
+		container_of(head, struct nfs_access_entry, rcu_head);
+
 	put_group_info(entry->group_info);
-	kfree_rcu(entry, rcu_head);
+	kfree(entry);
+}
+
+static void nfs_access_free_entry(struct nfs_access_entry *entry)
+{
+	/* RCU readers may still be comparing entry->group_info in access_cmp() */
+	call_rcu(&entry->rcu_head, nfs_access_free_entry_rcu);
 	smp_mb__before_atomic();
 	atomic_long_dec(&nfs_access_nr_entries);
 	smp_mb__after_atomic();

base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f

Signature · 1 line
2.34.1