Skip to contentSkip to conversation

nfsd/sunrpc: cleanup resource with sync mode

PATCH In review Yang Erkun 5 participants 11 messages Dec 25, 2024 – Jan 8, 2025

Subsystems (4)
lore
Share & tools
lore

Yang Erkun

author
#0 lore raw

v2 cover nfsd/sunrpc: cleanup resource with sync mode

After f8c989a0c89a ("nfsd: release svc_expkey/svc_export with rcu_work"), svc_export_put/expkey_put will call path_put with async mode. This can lead some unexpected failure:

mkdir /mnt/sda
mkfs.xfs -f /dev/sda
echo "/ *(rw,no_root_squash,fsid=0)" > /etc/exports echo "/mnt *(rw,no_root_squash,fsid=1)" >> /etc/exports exportfs -ra
service nfs-server start
mount -t nfs -o vers=4.0 127.0.0.1:/mnt /mnt1 mount /dev/sda /mnt/sda
touch /mnt1/sda/file
exportfs -r
umount /mnt/sda # failed unexcepted

The touch above will finally call nfsd_cross_mnt, add refcount to mount, and then add cache_head. Before this commit, exportfs -r will call cache_flush to cleanup all cache_head, and path_put in svc_export_put/expkey_put will be finished with sync mode. So, the latter umount will always success. However, after this commit, path_put will be called with async mode, the latter umount may failed, and if we add some delay, umount will success too. Personally I think this bug and should be fixed. We first revert before bugfix patch, and then fix the original bug with a different way.

v1->v2:

1. do not change cache_check, instead add cache_check_rcu and use it in c_show/e_show
2. the first patch has been applied

Yang Erkun (4):
SUNRPC: introduce cache_check_rcu to help check in rcu context
nfsd: no need get cache ref when protected by rcu
SUNRPC: no need get cache ref when protected by rcu
nfsd: fix UAF when access ex_uuid or ex_stats

 fs/nfsd/export.c             | 25 ++++++++++-------
 include/linux/sunrpc/cache.h |  2 ++
 net/sunrpc/cache.c           | 53 ++++++++++++++++++++----------------
 3 files changed, 46 insertions(+), 34 deletions(-)
Signature · 1 line
2.46.1
author
#1 lore raw

PATCH v2 1/4 SUNRPC: introduce cache_check_rcu to help check in rcu context

This is a prepare patch to add cache_check_rcu, will use it with follow patch.

---
 include/linux/sunrpc/cache.h |  2 ++
 net/sunrpc/cache.c           | 41 +++++++++++++++++++++++-------------
 2 files changed, 28 insertions(+), 15 deletions(-)
include/linux/sunrpc/cache.h +2 -0
index 35766963dd14..e783132e481f 100644
@@ -222,6 +222,8 @@ static inline bool cache_is_expired(struct cache_detail *detail, struct cache_he
 	return detail->flush_time >= h->last_refresh;
 }
 
+extern int cache_check_rcu(struct cache_detail *detail,
+		       struct cache_head *h, struct cache_req *rqstp);
 extern int cache_check(struct cache_detail *detail,
 		       struct cache_head *h, struct cache_req *rqstp);
 extern void cache_flush(void);
net/sunrpc/cache.c +26 -15

53 lines not on this page · raw message

Signature · 1 line
2.46.1
author
#2 lore raw

PATCH v2 2/4 nfsd: no need get cache ref when protected by rcu

In reply to Yang Erkun

rcu_read_lock/rcu_read_unlock has already provide protection for the pointer we will reference when we call e_show. Therefore, there is no need to obtain a cache reference to help protect cache_head. Additionally, the .put such as expkey_put/svc_export_put will invoke dput, which can sleep and break rcu. Stop get cache reference to fix them all.

Fixes ae74136b4bb6 ("SUNRPC: Allow cache lookups to use RCU protection rather than the r/w spinlock")
---
 fs/nfsd/export.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
fs/nfsd/export.c +1 -5
index aa4712362b3b..c6168bccfb6c 100644
@@ -1425,13 +1425,9 @@ static int e_show(struct seq_file *m, void *p)
 		return 0;
 	}
 
-	if (!cache_get_rcu(&exp->h))
+	if (cache_check_rcu(cd, &exp->h, NULL))
 		return 0;
 
-	if (cache_check(cd, &exp->h, NULL))
-		return 0;
-
-	exp_put(exp);
 	return svc_export_show(m, cd, cp);
 }
 
Signature · 1 line
2.46.1
author
#3 lore raw

PATCH v2 3/4 SUNRPC: no need get cache ref when protected by rcu

In reply to Yang Erkun

rcu_read_lock/rcu_read_unlock has already provide protection for the pointer we will reference when we call c_show. Therefore, there is no need to obtain a cache reference to help protect cache_head. Additionally, the .put such as expkey_put/svc_export_put will invoke dput, which can sleep and break rcu. Stop get cache reference to fix them all.

Fixes ae74136b4bb6 ("SUNRPC: Allow cache lookups to use RCU protection rather than the r/w spinlock")
---
 net/sunrpc/cache.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
net/sunrpc/cache.c +3 -9
index 88f42a27c8cc..cb279eb9ac4b 100644
@@ -1438,17 +1438,11 @@ static int c_show(struct seq_file *m, void *p)
 		seq_printf(m, "# expiry=%lld refcnt=%d flags=%lx\n",
 			   convert_to_wallclock(cp->expiry_time),
 			   kref_read(&cp->ref), cp->flags);
-	if (!cache_get_rcu(cp))
-		return 0;
 
-	if (cache_check(cd, cp, NULL))
-		/* cache_check does a cache_put on failure */
+	if (cache_check_rcu(cd, cp, NULL))
+		seq_puts(m, "# ");
+	else if (cache_is_expired(cd, cp))
 		seq_puts(m, "# ");
-	else {
-		if (cache_is_expired(cd, cp))
-			seq_puts(m, "# ");
-		cache_put(cp, cd);
-	}
 
 	return cd->cache_show(m, cd, cp);
 }
Signature · 1 line
2.46.1
author
#4 lore raw

PATCH v2 4/4 nfsd: fix UAF when access ex_uuid or ex_stats

In reply to Yang Erkun

We can access exp->ex_stats or exp->ex_uuid in rcu context(c_show and e_show). All these resources should be released using kfree_rcu. Fix this by using call_rcu, clean them all after a rcu grace period.

==================================================================

BUG: KASAN: slab-use-after-free in svc_export_show+0x362/0x430 [nfsd]
Read of size 1 at addr ff11000010fdc120 by task cat/870

CPU: 1 UID: 0 PID: 870 Comm: cat Not tainted 6.12.0-rc3+ #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
1.16.1-2.fc37 04/01/2014
Call Trace:
 <TASK>
 dump_stack_lvl+0x53/0x70
 print_address_description.constprop.0+0x2c/0x3a0
 print_report+0xb9/0x280
 kasan_report+0xae/0xe0
 svc_export_show+0x362/0x430 [nfsd]
 c_show+0x161/0x390 [sunrpc]
 seq_read_iter+0x589/0x770
 seq_read+0x1e5/0x270
 proc_reg_read+0xe1/0x140
 vfs_read+0x125/0x530
 ksys_read+0xc1/0x160
 do_syscall_64+0x5f/0x170
 entry_SYSCALL_64_after_hwframe+0x76/0x7e

Allocated by task 830:
 kasan_save_stack+0x20/0x40
 kasan_save_track+0x14/0x30
 __kasan_kmalloc+0x8f/0xa0
 __kmalloc_node_track_caller_noprof+0x1bc/0x400
 kmemdup_noprof+0x22/0x50
 svc_export_parse+0x8a9/0xb80 [nfsd]
 cache_do_downcall+0x71/0xa0 [sunrpc]
 cache_write_procfs+0x8e/0xd0 [sunrpc]
 proc_reg_write+0xe1/0x140
 vfs_write+0x1a5/0x6d0
 ksys_write+0xc1/0x160
 do_syscall_64+0x5f/0x170
 entry_SYSCALL_64_after_hwframe+0x76/0x7e

Freed by task 868:
 kasan_save_stack+0x20/0x40
 kasan_save_track+0x14/0x30
 kasan_save_free_info+0x3b/0x60
 __kasan_slab_free+0x37/0x50
 kfree+0xf3/0x3e0
 svc_export_put+0x87/0xb0 [nfsd]
 cache_purge+0x17f/0x1f0 [sunrpc]
 nfsd_destroy_serv+0x226/0x2d0 [nfsd]
 nfsd_svc+0x125/0x1e0 [nfsd]
 write_threads+0x16a/0x2a0 [nfsd]
 nfsctl_transaction_write+0x74/0xa0 [nfsd]
 vfs_write+0x1a5/0x6d0
 ksys_write+0xc1/0x160
 do_syscall_64+0x5f/0x170
 entry_SYSCALL_64_after_hwframe+0x76/0x7e
Fixes ae74136b4bb6 ("SUNRPC: Allow cache lookups to use RCU protection rather than the r/w spinlock")
---
 fs/nfsd/export.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/nfsd/export.c +14 -5
index c6168bccfb6c..0363720280d4 100644
@@ -355,16 +355,25 @@ static void export_stats_destroy(struct export_stats *stats)
 					    EXP_STATS_COUNTERS_NUM);
 }
 
-static void svc_export_put(struct kref *ref)
+static void svc_export_release(struct rcu_head *rcu_head)
 {
-	struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
-	path_put(&exp->ex_path);
-	auth_domain_put(exp->ex_client);
+	struct svc_export *exp = container_of(rcu_head, struct svc_export,
+			ex_rcu);
+
 	nfsd4_fslocs_free(&exp->ex_fslocs);
 	export_stats_destroy(exp->ex_stats);
 	kfree(exp->ex_stats);
 	kfree(exp->ex_uuid);
-	kfree_rcu(exp, ex_rcu);
+	kfree(exp);
+}
+
+static void svc_export_put(struct kref *ref)
+{
+	struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
+
+	path_put(&exp->ex_path);
+	auth_domain_put(exp->ex_client);
+	call_rcu(&exp->ex_rcu, svc_export_release);
 }
 
 static int svc_export_upcall(struct cache_detail *cd, struct cache_head *h)
Signature · 1 line
2.46.1
#5 lore raw

Re: [PATCH v2 4/4] nfsd: fix UAF when access ex_uuid or ex_stats

Quoted 90 lines · Yang Erkun

We can access exp->ex_stats or exp->ex_uuid in rcu context(c_show and e_show). All these resources should be released using kfree_rcu. Fix this by using call_rcu, clean them all after a rcu grace period.

==================================================================

BUG: KASAN: slab-use-after-free in svc_export_show+0x362/0x430 [nfsd]
Read of size 1 at addr ff11000010fdc120 by task cat/870

CPU: 1 UID: 0 PID: 870 Comm: cat Not tainted 6.12.0-rc3+ #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
1.16.1-2.fc37 04/01/2014
Call Trace:
 <TASK>
 dump_stack_lvl+0x53/0x70
 print_address_description.constprop.0+0x2c/0x3a0
 print_report+0xb9/0x280
 kasan_report+0xae/0xe0
 svc_export_show+0x362/0x430 [nfsd]
 c_show+0x161/0x390 [sunrpc]
 seq_read_iter+0x589/0x770
 seq_read+0x1e5/0x270
 proc_reg_read+0xe1/0x140
 vfs_read+0x125/0x530
 ksys_read+0xc1/0x160
 do_syscall_64+0x5f/0x170
 entry_SYSCALL_64_after_hwframe+0x76/0x7e

Allocated by task 830:
 kasan_save_stack+0x20/0x40
 kasan_save_track+0x14/0x30
 __kasan_kmalloc+0x8f/0xa0
 __kmalloc_node_track_caller_noprof+0x1bc/0x400
 kmemdup_noprof+0x22/0x50
 svc_export_parse+0x8a9/0xb80 [nfsd]
 cache_do_downcall+0x71/0xa0 [sunrpc]
 cache_write_procfs+0x8e/0xd0 [sunrpc]
 proc_reg_write+0xe1/0x140
 vfs_write+0x1a5/0x6d0
 ksys_write+0xc1/0x160
 do_syscall_64+0x5f/0x170
 entry_SYSCALL_64_after_hwframe+0x76/0x7e

Freed by task 868:
 kasan_save_stack+0x20/0x40
 kasan_save_track+0x14/0x30
 kasan_save_free_info+0x3b/0x60
 __kasan_slab_free+0x37/0x50
 kfree+0xf3/0x3e0
 svc_export_put+0x87/0xb0 [nfsd]
 cache_purge+0x17f/0x1f0 [sunrpc]
 nfsd_destroy_serv+0x226/0x2d0 [nfsd]
 nfsd_svc+0x125/0x1e0 [nfsd]
 write_threads+0x16a/0x2a0 [nfsd]
 nfsctl_transaction_write+0x74/0xa0 [nfsd]
 vfs_write+0x1a5/0x6d0
 ksys_write+0xc1/0x160
 do_syscall_64+0x5f/0x170
 entry_SYSCALL_64_after_hwframe+0x76/0x7e
Fixes ae74136b4bb6 ("SUNRPC: Allow cache lookups to use RCU protection rather than the r/w spinlock")
---
 fs/nfsd/export.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/nfsd/export.c +6 -5
index c6168bccfb6c..0363720280d4 100644
@@ -355,16 +355,25 @@ static void export_stats_destroy(struct export_stats *stats)
 					    EXP_STATS_COUNTERS_NUM);
 }
 
-static void svc_export_put(struct kref *ref)
+static void svc_export_release(struct rcu_head *rcu_head)
 {
-	struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
-	path_put(&exp->ex_path);
-	auth_domain_put(exp->ex_client);
+	struct svc_export *exp = container_of(rcu_head, struct svc_export,
+			ex_rcu);
+
 	nfsd4_fslocs_free(&exp->ex_fslocs);
 	export_stats_destroy(exp->ex_stats);
 	kfree(exp->ex_stats);
 	kfree(exp->ex_uuid);
-	kfree_rcu(exp, ex_rcu);
+	kfree(exp);
+}

fs/nfsd/export.c static void export_stats_destroy(struct export_stats *stats) · line 378

Quoted 10 lines
fs/nfsd/export.c +8 -0
+
+static void svc_export_put(struct kref *ref)
+{
+	struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
+
+	path_put(&exp->ex_path);
+	auth_domain_put(exp->ex_client);
+	call_rcu(&exp->ex_rcu, svc_export_release);
 }
 

I think that ip_map_put() needs to be fixed for the same reason that svc_export_put() needed to be fixed.

ip_map_put() calls auth_domain_put() in ->im_client immediately, but ip_map_show() accesses ->im_client.
So ip_map_put() needs to delay the auth_domain_put() using rcu.

These two fixes should really be *before* patch 3/4 as without these fixes 3/4 introduces a bug.

Thanks,
NeilBrown

Thanks,

#6 lore raw

Re: [PATCH v2 4/4] nfsd: fix UAF when access ex_uuid or ex_stats

Quoted 105 lines · NeilBrown
Quoted 90 lines · Yang Erkun

We can access exp->ex_stats or exp->ex_uuid in rcu context(c_show and e_show). All these resources should be released using kfree_rcu. Fix this by using call_rcu, clean them all after a rcu grace period.

==================================================================

BUG: KASAN: slab-use-after-free in svc_export_show+0x362/0x430 [nfsd]
Read of size 1 at addr ff11000010fdc120 by task cat/870

CPU: 1 UID: 0 PID: 870 Comm: cat Not tainted 6.12.0-rc3+ #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
1.16.1-2.fc37 04/01/2014
Call Trace:
  <TASK>
  dump_stack_lvl+0x53/0x70
  print_address_description.constprop.0+0x2c/0x3a0
  print_report+0xb9/0x280
  kasan_report+0xae/0xe0
  svc_export_show+0x362/0x430 [nfsd]
  c_show+0x161/0x390 [sunrpc]
  seq_read_iter+0x589/0x770
  seq_read+0x1e5/0x270
  proc_reg_read+0xe1/0x140
  vfs_read+0x125/0x530
  ksys_read+0xc1/0x160
  do_syscall_64+0x5f/0x170
  entry_SYSCALL_64_after_hwframe+0x76/0x7e

Allocated by task 830:
  kasan_save_stack+0x20/0x40
  kasan_save_track+0x14/0x30
  __kasan_kmalloc+0x8f/0xa0
  __kmalloc_node_track_caller_noprof+0x1bc/0x400
  kmemdup_noprof+0x22/0x50
  svc_export_parse+0x8a9/0xb80 [nfsd]
  cache_do_downcall+0x71/0xa0 [sunrpc]
  cache_write_procfs+0x8e/0xd0 [sunrpc]
  proc_reg_write+0xe1/0x140
  vfs_write+0x1a5/0x6d0
  ksys_write+0xc1/0x160
  do_syscall_64+0x5f/0x170
  entry_SYSCALL_64_after_hwframe+0x76/0x7e

Freed by task 868:
  kasan_save_stack+0x20/0x40
  kasan_save_track+0x14/0x30
  kasan_save_free_info+0x3b/0x60
  __kasan_slab_free+0x37/0x50
  kfree+0xf3/0x3e0
  svc_export_put+0x87/0xb0 [nfsd]
  cache_purge+0x17f/0x1f0 [sunrpc]
  nfsd_destroy_serv+0x226/0x2d0 [nfsd]
  nfsd_svc+0x125/0x1e0 [nfsd]
  write_threads+0x16a/0x2a0 [nfsd]
  nfsctl_transaction_write+0x74/0xa0 [nfsd]
  vfs_write+0x1a5/0x6d0
  ksys_write+0xc1/0x160
  do_syscall_64+0x5f/0x170
  entry_SYSCALL_64_after_hwframe+0x76/0x7e
Fixes ae74136b4bb6 ("SUNRPC: Allow cache lookups to use RCU protection rather than the r/w spinlock")
---
  fs/nfsd/export.c | 19 ++++++++++++++-----
  1 file changed, 14 insertions(+), 5 deletions(-)
fs/nfsd/export.c +6 -5
index c6168bccfb6c..0363720280d4 100644
@@ -355,16 +355,25 @@ static void export_stats_destroy(struct export_stats *stats)
  					    EXP_STATS_COUNTERS_NUM);
  }
 
-static void svc_export_put(struct kref *ref)
+static void svc_export_release(struct rcu_head *rcu_head)
  {
-	struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
-	path_put(&exp->ex_path);
-	auth_domain_put(exp->ex_client);
+	struct svc_export *exp = container_of(rcu_head, struct svc_export,
+			ex_rcu);
+
  	nfsd4_fslocs_free(&exp->ex_fslocs);
  	export_stats_destroy(exp->ex_stats);
  	kfree(exp->ex_stats);
  	kfree(exp->ex_uuid);
-	kfree_rcu(exp, ex_rcu);
+	kfree(exp);
+}

fs/nfsd/export.c static void export_stats_destroy(struct export_stats *stats) · line 378

Quoted 10 lines
fs/nfsd/export.c +8 -0
+
+static void svc_export_put(struct kref *ref)
+{
+	struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
+
+	path_put(&exp->ex_path);
+	auth_domain_put(exp->ex_client);
+	call_rcu(&exp->ex_rcu, svc_export_release);
  }
 

I think that ip_map_put() needs to be fixed for the same reason that svc_export_put() needed to be fixed.

Quoted 3 lines

ip_map_put() calls auth_domain_put() in ->im_client immediately, but ip_map_show() accesses ->im_client.
So ip_map_put() needs to delay the auth_domain_put() using rcu.

Thanks for your detail review!

auth_domain_put will eventually call auth_domain_release, which all .domain_release will call call_rcu to free auth_domain. Maybe it is safe now?

Quoted 8 lines

These two fixes should really be *before* patch 3/4 as without these fixes 3/4 introduces a bug.

Thanks,
NeilBrown

Thanks,

#7 lore raw

Re: [PATCH v2 4/4] nfsd: fix UAF when access ex_uuid or ex_stats

Quoted 111 lines · yangerkun
Quoted 105 lines · NeilBrown
Quoted 90 lines · Yang Erkun

We can access exp->ex_stats or exp->ex_uuid in rcu context(c_show and e_show). All these resources should be released using kfree_rcu. Fix this by using call_rcu, clean them all after a rcu grace period.

==================================================================

BUG: KASAN: slab-use-after-free in svc_export_show+0x362/0x430 [nfsd]
Read of size 1 at addr ff11000010fdc120 by task cat/870

CPU: 1 UID: 0 PID: 870 Comm: cat Not tainted 6.12.0-rc3+ #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
1.16.1-2.fc37 04/01/2014
Call Trace:
  <TASK>
  dump_stack_lvl+0x53/0x70
  print_address_description.constprop.0+0x2c/0x3a0
  print_report+0xb9/0x280
  kasan_report+0xae/0xe0
  svc_export_show+0x362/0x430 [nfsd]
  c_show+0x161/0x390 [sunrpc]
  seq_read_iter+0x589/0x770
  seq_read+0x1e5/0x270
  proc_reg_read+0xe1/0x140
  vfs_read+0x125/0x530
  ksys_read+0xc1/0x160
  do_syscall_64+0x5f/0x170
  entry_SYSCALL_64_after_hwframe+0x76/0x7e

Allocated by task 830:
  kasan_save_stack+0x20/0x40
  kasan_save_track+0x14/0x30
  __kasan_kmalloc+0x8f/0xa0
  __kmalloc_node_track_caller_noprof+0x1bc/0x400
  kmemdup_noprof+0x22/0x50
  svc_export_parse+0x8a9/0xb80 [nfsd]
  cache_do_downcall+0x71/0xa0 [sunrpc]
  cache_write_procfs+0x8e/0xd0 [sunrpc]
  proc_reg_write+0xe1/0x140
  vfs_write+0x1a5/0x6d0
  ksys_write+0xc1/0x160
  do_syscall_64+0x5f/0x170
  entry_SYSCALL_64_after_hwframe+0x76/0x7e

Freed by task 868:
  kasan_save_stack+0x20/0x40
  kasan_save_track+0x14/0x30
  kasan_save_free_info+0x3b/0x60
  __kasan_slab_free+0x37/0x50
  kfree+0xf3/0x3e0
  svc_export_put+0x87/0xb0 [nfsd]
  cache_purge+0x17f/0x1f0 [sunrpc]
  nfsd_destroy_serv+0x226/0x2d0 [nfsd]
  nfsd_svc+0x125/0x1e0 [nfsd]
  write_threads+0x16a/0x2a0 [nfsd]
  nfsctl_transaction_write+0x74/0xa0 [nfsd]
  vfs_write+0x1a5/0x6d0
  ksys_write+0xc1/0x160
  do_syscall_64+0x5f/0x170
  entry_SYSCALL_64_after_hwframe+0x76/0x7e
Fixes ae74136b4bb6 ("SUNRPC: Allow cache lookups to use RCU protection rather than the r/w spinlock")
---
  fs/nfsd/export.c | 19 ++++++++++++++-----
  1 file changed, 14 insertions(+), 5 deletions(-)
fs/nfsd/export.c +6 -5
index c6168bccfb6c..0363720280d4 100644
@@ -355,16 +355,25 @@ static void export_stats_destroy(struct export_stats *stats)
  					    EXP_STATS_COUNTERS_NUM);
  }
 
-static void svc_export_put(struct kref *ref)
+static void svc_export_release(struct rcu_head *rcu_head)
  {
-	struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
-	path_put(&exp->ex_path);
-	auth_domain_put(exp->ex_client);
+	struct svc_export *exp = container_of(rcu_head, struct svc_export,
+			ex_rcu);
+
  	nfsd4_fslocs_free(&exp->ex_fslocs);
  	export_stats_destroy(exp->ex_stats);
  	kfree(exp->ex_stats);
  	kfree(exp->ex_uuid);
-	kfree_rcu(exp, ex_rcu);
+	kfree(exp);
+}

fs/nfsd/export.c static void export_stats_destroy(struct export_stats *stats) · line 378

Quoted 10 lines
fs/nfsd/export.c +8 -0
+
+static void svc_export_put(struct kref *ref)
+{
+	struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
+
+	path_put(&exp->ex_path);
+	auth_domain_put(exp->ex_client);
+	call_rcu(&exp->ex_rcu, svc_export_release);
  }
 

I think that ip_map_put() needs to be fixed for the same reason that svc_export_put() needed to be fixed.

Quoted 3 lines

ip_map_put() calls auth_domain_put() in ->im_client immediately, but ip_map_show() accesses ->im_client.
So ip_map_put() needs to delay the auth_domain_put() using rcu.

Quoted 5 lines

Thanks for your detail review!

auth_domain_put will eventually call auth_domain_release, which all .domain_release will call call_rcu to free auth_domain. Maybe it is safe now?

Yes, that makes sense. As you say, no change is needed for ip_map_put().

Thanks,
NeilBrown

#8 lore raw
In reply to Yang Erkun

From: Chuck Lever <[email protected]>

Quoted 16 lines · Yang Erkun

After f8c989a0c89a ("nfsd: release svc_expkey/svc_export with rcu_work"), svc_export_put/expkey_put will call path_put with async mode. This can lead some unexpected failure:

mkdir /mnt/sda
mkfs.xfs -f /dev/sda
echo "/ *(rw,no_root_squash,fsid=0)" > /etc/exports echo "/mnt *(rw,no_root_squash,fsid=1)" >> /etc/exports exportfs -ra
service nfs-server start
mount -t nfs -o vers=4.0 127.0.0.1:/mnt /mnt1 mount /dev/sda /mnt/sda
touch /mnt1/sda/file
exportfs -r
umount /mnt/sda # failed unexcepted

Quoted 1 line

[...]

Applied to nfsd-testing for v6.14, thanks!

I'm unsure whether 2/4 and 3/4 need Fixes: tags. Based on the patch descriptions, these appear to be pre-requisite patches rather than actual fixes.

However, I'm guessing that 4/4 will apply and build without 2/4 and 3/4 applied, but there will be operational problems. So explicit
Fixes: tags in 2/4 and 3/4 are probably necessary to get a properly working backport.

This is a case where I would rather have manual backporting with a full CI run rather than just slapping these patches on the LTS kernels and hoping for the best.

[1/4] SUNRPC: introduce cache_check_rcu to help check in rcu context

      commit: 401fd94cb21c978403077d611bdc311dc8376f9d
[2/4] nfsd: no need get cache ref when protected by rcu
      commit: 3d32486aa35bf4b99edbfc27538b24fadebd7f75
[3/4] SUNRPC: no need get cache ref when protected by rcu
      commit: 9f5fc67903b5709c4334bf33764d6fbf0ceb05b4
[4/4] nfsd: fix UAF when access ex_uuid or ex_stats
      commit: 8fa5adbee5ab904ea282da183ece664a67db2b2b

--
Chuck Lever

maintainer Reviewed-by on the v2 series
#9 lore raw
In reply to Yang Erkun
Quoted 38 lines · Yang Erkun

After f8c989a0c89a ("nfsd: release svc_expkey/svc_export with rcu_work"), svc_export_put/expkey_put will call path_put with async mode. This can lead some unexpected failure:

mkdir /mnt/sda
mkfs.xfs -f /dev/sda
echo "/ *(rw,no_root_squash,fsid=0)" > /etc/exports echo "/mnt *(rw,no_root_squash,fsid=1)" >> /etc/exports exportfs -ra
service nfs-server start
mount -t nfs -o vers=4.0 127.0.0.1:/mnt /mnt1 mount /dev/sda /mnt/sda
touch /mnt1/sda/file
exportfs -r
umount /mnt/sda # failed unexcepted

The touch above will finally call nfsd_cross_mnt, add refcount to mount, and then add cache_head. Before this commit, exportfs -r will call cache_flush to cleanup all cache_head, and path_put in svc_export_put/expkey_put will be finished with sync mode. So, the latter umount will always success. However, after this commit, path_put will be called with async mode, the latter umount may failed, and if we add some delay, umount will success too. Personally I think this bug and should be fixed. We first revert before bugfix patch, and then fix the original bug with a different way.

v1->v2:

1. do not change cache_check, instead add cache_check_rcu and use it in c_show/e_show
2. the first patch has been applied

Yang Erkun (4):
SUNRPC: introduce cache_check_rcu to help check in rcu context
nfsd: no need get cache ref when protected by rcu
SUNRPC: no need get cache ref when protected by rcu
nfsd: fix UAF when access ex_uuid or ex_stats

Quoted 5 lines
 fs/nfsd/export.c             | 25 ++++++++++-------
 include/linux/sunrpc/cache.h |  2 ++
 net/sunrpc/cache.c           | 53 ++++++++++++++++++++----------------
 3 files changed, 46 insertions(+), 34 deletions(-)

A much nicer fix. My only minor nit is that it might be nice to add a kerneldoc header over cache_check_rcu, so that it's clear that it doesn't touch the cache_head refcount like cache_check does.

Either way, you can add:

#10 lore raw
In reply to Yang Erkun

From: Chuck Lever <[email protected]>

Quoted 16 lines · Yang Erkun

After f8c989a0c89a ("nfsd: release svc_expkey/svc_export with rcu_work"), svc_export_put/expkey_put will call path_put with async mode. This can lead some unexpected failure:

mkdir /mnt/sda
mkfs.xfs -f /dev/sda
echo "/ *(rw,no_root_squash,fsid=0)" > /etc/exports echo "/mnt *(rw,no_root_squash,fsid=1)" >> /etc/exports exportfs -ra
service nfs-server start
mount -t nfs -o vers=4.0 127.0.0.1:/mnt /mnt1 mount /dev/sda /mnt/sda
touch /mnt1/sda/file
exportfs -r
umount /mnt/sda # failed unexcepted

Quoted 1 line

[...]

Applied to nfsd-testing, thanks!

[1/4] SUNRPC: introduce cache_check_rcu to help check in rcu context

      commit: 2f55dbe4e2072c9e99298c6f37473778a98c9107
[2/4] nfsd: no need get cache ref when protected by rcu
      commit: c224edca7af028828e2ad866b61d731b5e72b46d
[3/4] SUNRPC: no need get cache ref when protected by rcu
      commit: 1b10f0b603c066d81327c163a23c19f01e112366
[4/4] nfsd: fix UAF when access ex_uuid or ex_stats
      commit: 2530766492ec7726582bcde44575ec3ff7487cd2

--
Chuck Lever