NFS: defer access-cache entry and group_info free past RCU
Subsystems (3)
PATCH v2 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. The lockless lookup path (nfs_access_get_cached_rcu() -> access_cmp()) can still be walking group_info->ngroups/gid[] after the entry was replaced and unlinked, so put_group_info() can reach groups_free() -> kvfree() under a reader and free the array it is
reading: a slab-use-after-free read.
put_group_info() may end in kvfree(), so it cannot run from a plain RCU callback. Defer both the group_info reference drop and the entry free to process context after a grace period with queue_rcu_work() on nfsiod_workqueue. The entry accounting (nfs_access_nr_entries) and all call-site locking are unchanged; the work only drops the reference and frees the entry.
On a 7.3 KASAN kernel, the following workload triggers the report: a userspace NFSv3 server plus eight readers doing faccessat() with 2048 supplementary gids, and serialized credential replacements that detach the previous killer's group_info reference.
BUG: KASAN: use-after-free in nfs_access_get_cached+0xb8f/0xf80 Read of size 4 at addr ... by task poc/9552 Call Trace: nfs_access_get_cached nfs_do_access nfs_permission inode_permission do_faccessat Page last allocated by task ...: __kvmalloc_node_noprof __se_sys_setgroups Page last freed by task ...: nfs_access_free_entry nfs_access_add_cache nfs_do_access
The trigger needs CAP_SETGID in the guest for the setgroups-based credential churn; the faulty read path is the ordinary access(2) / faccessat(2) path used by any NFS user.
---
v2: add Link, Fixes and stable tags, and remove local validation details from the commit message.
fs/nfs/dir.c | 21 +++++++++++++++++---- include/linux/nfs_fs.h | 3 ++- 2 files changed, 19 insertions(+), 5 deletions(-)
fs/nfs/dir.c +17 -2
@@ -2898,10 +2898,25 @@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_work (struct work_struct *work) { +struct nfs_access_entry *entry = + container_of(to_rcu_work(work), struct nfs_access_entry, rcu_work); +put_group_info (entry->group_info); -kfree_rcu (entry, rcu_head); +kfree (entry); +} + +static void nfs_access_free_entry (struct nfs_access_entry *entry) +{ +/* +* The entry can still be reached by RCU readers that dereference +* its group_info in access_cmp(). Drop the group_info reference +* only after a grace period, in process context because +* put_group_info() may end in kvfree(). +*/ +INIT_RCU_WORK (&entry->rcu_work, nfs_access_free_entry_work); +queue_rcu_work (nfsiod_workqueue, &entry->rcu_work);smp_mb__before_atomic ();atomic_long_dec (&nfs_access_nr_entries);smp_mb__after_atomic ();
include/linux/nfs_fs.h +2 -1
@@ -26,6 +26,7 @@#include <linux/refcount.h> #include <linux/rwsem.h> #include <linux/wait.h> +#include <linux/workqueue.h> #include <linux/sunrpc/debug.h> #include <linux/sunrpc/auth.h>
@@ -65,7 +66,7 @@struct group_info *group_info;u64 timestamp;__u32 mask; -struct rcu_head rcu_head; +struct rcu_work rcu_work; };struct nfs_lock_context {
Trond Myklebust
maintainerQuoted 89 lines · Cheng Yongkang
nfs_access_free_entry()drops thegroup_inforeference synchronously while thenfs_access_entryitself is only freed after an RCU grace period. The lockless lookup path (nfs_access_get_cached_rcu()->access_cmp()) can still be walking group_info->ngroups/gid[] after the
entry was replaced and unlinked, soput_group_info()can reachgroups_free()->kvfree()under a reader and free the array it is
reading: a slab-use-after-free read.
put_group_info()may end inkvfree(), so it cannot run from a plain RCU
callback. Defer both thegroup_inforeference drop and the entry free
to process context after a grace period withqueue_rcu_work()on nfsiod_workqueue. The entry accounting (nfs_access_nr_entries) and all
call-site locking are unchanged; the work only drops the reference and
frees the entry.On a 7.3 KASAN kernel, the following workload triggers the report: a userspace NFSv3 server plus eight readers doing
faccessat()with 2048 supplementary gids, and serialized credential replacements that detach
the previous killer'sgroup_inforeference.BUG: KASAN: use-after-free in nfs_access_get_cached+0xb8f/0xf80 Read of size 4 at addr ... by task poc/9552 Call Trace:
nfs_access_get_cached
nfs_do_access
nfs_permission
inode_permission
do_faccessat
Page last allocated by task ...:
__kvmalloc_node_noprof
__se_sys_setgroups
Page last freed by task ...:
nfs_access_free_entry
nfs_access_add_cache
nfs_do_accessThe trigger needs
CAP_SETGIDin the guest for the setgroups-based credential churn; the faulty read path is the ordinary access(2) / faccessat(2) path used by any NFS user.Link:
https://lore.kernel.org/all/[email protected]/Fixes 6238aec83f3f ("NFS: don't store 'struct cred *' in struct
nfs_access_entry")---
v2: add Link, Fixes and stable tags, and remove local validation details from the
commit message.fs/nfs/dir.c | 21 +++++++++++++++++----
include/linux/nfs_fs.h| 3 ++-2 files changed, 19 insertions(+), 5 deletions(-)fs/nfs/dir.c +0
-0@@ -2898,10 +2898,25 @@ module_param(nfs_access_max_cachesize, ulong, 0644);MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
fs/nfs/dir.c +1
-1-static void nfs_access_free_entry (struct nfs_access_entry *entry) +static void nfs_access_free_entry_work (struct work_struct *work){
fs/nfs/dir.c +2
-0+struct nfs_access_entry *entry = + container_of(to_rcu_work(work), struct
nfs_access_entry,rcu_work);
+
put_group_info(entry->group_info);fs/nfs/dir.c +7
-1-kfree_rcu (entry, rcu_head); +kfree (entry); +} + +static void nfs_access_free_entry (struct nfs_access_entry *entry) +{ +/* +* The entry can still be reached by RCU readers that dereference
fs/nfs/dir.c +1
-0+* its group_info in access_cmp(). Drop the group_info reference
fs/nfs/dir.c line 2919
Quoted 5 lines
fs/nfs/dir.c +5
-0+* only after a grace period, in process context because +* put_group_info() may end in kvfree(). +*/ +INIT_RCU_WORK (&entry->rcu_work, nfs_access_free_entry_work); +queue_rcu_work (nfsiod_workqueue, &entry->rcu_work);
Why is this a workqueue job and not just an RCU callback?
Signature · 3 lines
Trond Myklebust Linux NFS client maintainer, Hammerspace [email protected], [email protected]
Quoted 4 lines · Trond Myklebust
fs/nfs/dir.c line 2919
Quoted 2 lines
fs/nfs/dir.c +2
-0+INIT_RCU_WORK (&entry->rcu_work, nfs_access_free_entry_work); +queue_rcu_work (nfsiod_workqueue, &entry->rcu_work);Why is this a workqueue job and not just an RCU callback?
There is no need for one. I assumed put_group_info() could not run from an RCU callback because groups_free() uses kvfree(), but kvfree() may be called in interrupt context: vfree() hands the area to vfree_atomic() there. put_cred_rcu() already drops a group_info reference from an RCU callback.
I'll send a v3 that frees the entry with call_rcu() and leaves struct nfs_access_entry unchanged.
Thanks,
Cheng