mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Cheng Yongkang <teel4res@gmail.com>
To: Trond Myklebust <trondmy@kernel.org>, Anna Schumaker <anna@kernel.org>
Cc: linux-nfs@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: [PATCH v3] NFS: defer access-cache entry and group_info free past RCU
Date: Sun, 29 Nov 2026 20:50:29 -0800	[thread overview]
Message-ID: <20261130045029.3414698-1-teel4res@gmail.com> (raw)

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")
Cc: stable@vger.kernel.org
Signed-off-by: Cheng Yongkang <teel4res@gmail.com>
---
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/20261026105230.1150218-1-teel4res@gmail.com/
v1: https://lore.kernel.org/all/20261025180329.349613-1-teel4res@gmail.com/

 fs/nfs/dir.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c
index 49437037f948..deed0dc49fc2 100644
--- a/fs/nfs/dir.c
+++ b/fs/nfs/dir.c
@@ -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
-- 
2.34.1


                 reply	other threads:[~2026-11-30  4:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130045029.3414698-1-teel4res@gmail.com \
    --to=teel4res@gmail.com \
    --cc=anna@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=trondmy@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®