Skip to contentSkip to conversation

lockd: prevent UAF in nlm4svc_proc_test in reexport NLM

RFC PATCH In review Olga Kornievskaia 4 participants 28 messages Oct 21, 2025 – Nov 3, 2025

Subsystems (4)
lore
Share & tools
lore

Olga Kornievskaia

author reviewer
#0 lore raw

RFC PATCH 1/1 lockd: prevent UAF in nlm4svc_proc_test in reexport NLM

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
 	conflock->fl.c.flc_type = lock->fl.c.flc_type;
 	conflock->fl.fl_start = lock->fl.fl_start;
 	conflock->fl.fl_end = lock->fl.fl_end;
+	conflock->fl.fl_lmops = NULL;
 	locks_release_private(&lock->fl);
 
 	ret = nlm_lck_denied;
Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 175 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +0 -0
index a31dc9588eb8..1dd0fec186de 100644

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 658

Quoted 7 lines
fs/lockd/svclock.c +1 -0
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
 	conflock->fl.c.flc_type = lock->fl.c.flc_type;
 	conflock->fl.fl_start = lock->fl.fl_start;
 	conflock->fl.fl_end = lock->fl.fl_end;
+	conflock->fl.fl_lmops = NULL;
 	locks_release_private(&lock->fl);
 
 	ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

Doesn't that mean that the real bug is that we're missing taking an owner reference in some case?

Signature · 1 line
Jeff Layton <[email protected]>
#2 lore raw
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 658

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 5 lines

Doesn't that mean that the real bug is that we're missing taking an owner reference in some case?
--
Jeff Layton <[email protected]>

#3 lore raw
Quoted 193 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 658

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 5 lines
Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

I realize that's not correct to say. What I meant to say is that we are not leaking a refcount by setting fl_lmops to null. There will be the last put in the nlm4svc_proc_test.

Quoted 34 lines

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 5 lines

Doesn't that mean that the real bug is that we're missing taking an owner reference in some case?
--
Jeff Layton <[email protected]>

maintainer
#4 lore raw
In reply to Olga Kornievskaia
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 658

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

Signature · 1 line
Jeff Layton <[email protected]>
#5 lore raw
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 658

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Quoted 3 lines

--
Jeff Layton <[email protected]>

maintainer
#6 lore raw
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 658

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +0 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock

*fl)

        if (status < 0)
                goto out;

+ locks_release_private(&fl);
+

        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;
Signature · 1 line
Jeff Layton <[email protected]>
maintainer
#7 lore raw
Quoted 292 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 658

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +2 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
Quoted 1 line
                        fl->c.flc_type = F_UNLCK;

Sorry, I take it back. That alone won't fix it because of the pointer- saving shenanigans in __nlm4svc_proc_test. We'll need to do this a bit more carefully, I think.

Signature · 1 line
Jeff Layton <[email protected]>
maintainer
#8 lore raw
Quoted 289 lines · Jeff Layton
Quoted 288 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 658

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +0 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
Quoted 10 lines
Quoted 5 lines
fs/lockd/clntproc.c +2 -0
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;

Sorry, I take it back. That alone won't fix it because of the pointer- saving shenanigans in __nlm4svc_proc_test. We'll need to do this a bit more carefully, I think.

How about this patch? The changelog still needs some work, but I think this is the most correct way to handle it:

-------------------------8<----------------------------

[PATCH] nlm: fix handling of conflocks by NLM

The handling of conflocks can result in a refcount overput on the flc_owner in the case of a reexported NFSv3 fs.

lockd will pass down a file_lock structure in its arguments with flc_owner set to an NLM owner and a valid reference. Once that gets down to the reexported NFS client, it will ignore that field and leave it intact when copying in conflock info.

The NLM server code will then end up releasing the conflock, and then releasing the one from the arguments, not realizing that the lock has already been released.

The owner info in the arguments to vfs_test_lock() is there to give information about the requestor. Once nlmclnt_test() is going to copy in the conflock info however, it needs to release the old owner.

Have nlmclnt_test() do call locks_release_private() after testing the lock to release any info that was in the old file_lock structure.

This creates another problem: __nlm4svc_proc_test() and __nlmsvc_proc_test() both call nlmsvc_put_lockowner() unconditionally on the original lockowner that it got from decoding the args. Switch them both to just call locks_release_private() on &argp->lock instead, which should handle the case properly where the info in argp->lock has already been released.

With that changed, we can also eliminate the call to locks_release_private() in nlmsvc_testlock() since the callers will now handle that.

---
 fs/lockd/clntproc.c | 3 +++
 fs/lockd/svc4proc.c | 4 +---
 fs/lockd/svclock.c  | 1 -
 fs/lockd/svcproc.c  | 5 +----
 4 files changed, 5 insertions(+), 8 deletions(-)
fs/lockd/clntproc.c +3 -0
index cebcc283b7ce..d3dd04137677 100644
@@ -438,6 +438,9 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
 	if (status < 0)
 		goto out;
 
+	/* Release any references held by fl before copying in conflock info */
+	locks_release_private(fl);
+
 	switch (req->a_res.status) {
 		case nlm_granted:
 			fl->c.flc_type = F_UNLCK;
fs/lockd/svc4proc.c +1 -3
index 109e5caae8c7..cbfec12296a4 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	struct nlm_args *argp = rqstp->rq_argp;
 	struct nlm_host	*host;
 	struct nlm_file	*file;
-	struct nlm_lockowner *test_owner;
 	__be32 rc = rpc_success;
 
 	dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-	test_owner = argp->lock.fl.c.flc_owner;
 	/* Now check for conflicting locks */
 	resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
 				       &resp->lock);
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	else
 		dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-	nlmsvc_put_lockowner(test_owner);
+	locks_release_private(&argp->lock.fl);
 	nlmsvc_release_host(host);
 	nlm_release_file(file);
 	return rc;
fs/lockd/svclock.c +0 -1
index a31dc9588eb8..7ff3d75a5ca3 100644
@@ -652,7 +652,6 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
 	conflock->fl.c.flc_type = lock->fl.c.flc_type;
 	conflock->fl.fl_start = lock->fl.fl_start;
 	conflock->fl.fl_end = lock->fl.fl_end;
-	locks_release_private(&lock->fl);
 
 	ret = nlm_lck_denied;
 out:
fs/lockd/svcproc.c +1 -4
index f53d5177f267..6f3e669a3532 100644
@@ -117,7 +117,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	struct nlm_args *argp = rqstp->rq_argp;
 	struct nlm_host	*host;
 	struct nlm_file	*file;
-	struct nlm_lockowner *test_owner;
 	__be32 rc = rpc_success;
 
 	dprintk("lockd: TEST          called\n");
@@ -127,8 +126,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-	test_owner = argp->lock.fl.c.flc_owner;
-
 	/* Now check for conflicting locks */
 	resp->status = cast_status(nlmsvc_testlock(rqstp, file, host,
 						   &argp->lock, &resp->lock));
@@ -138,7 +135,7 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 		dprintk("lockd: TEST          status %d vers %d\n",
 			ntohl(resp->status), rqstp->rq_vers);
 
-	nlmsvc_put_lockowner(test_owner);
+	locks_release_private(&argp->lock.fl);
 	nlmsvc_release_host(host);
 	nlm_release_file(file);
 	return rc;
Signature · 1 line
2.51.0
#9 lore raw
Quoted 425 lines · Jeff Layton
Quoted 289 lines · Jeff Layton
Quoted 288 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 656

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +0 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
Quoted 10 lines
Quoted 5 lines
fs/lockd/clntproc.c +2 -0
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;

Sorry, I take it back. That alone won't fix it because of the pointer- saving shenanigans in __nlm4svc_proc_test. We'll need to do this a bit more carefully, I think.

How about this patch? The changelog still needs some work, but I think this is the most correct way to handle it:

-------------------------8<----------------------------

[PATCH] nlm: fix handling of conflocks by NLM

The handling of conflocks can result in a refcount overput on the flc_owner in the case of a reexported NFSv3 fs.

lockd will pass down a file_lock structure in its arguments with flc_owner set to an NLM owner and a valid reference. Once that gets down to the reexported NFS client, it will ignore that field and leave it intact when copying in conflock info.

The NLM server code will then end up releasing the conflock, and then releasing the one from the arguments, not realizing that the lock has already been released.

The owner info in the arguments to vfs_test_lock() is there to give information about the requestor. Once nlmclnt_test() is going to copy in the conflock info however, it needs to release the old owner.

Have nlmclnt_test() do call locks_release_private() after testing the lock to release any info that was in the old file_lock structure.

This creates another problem: __nlm4svc_proc_test() and __nlmsvc_proc_test() both call nlmsvc_put_lockowner() unconditionally on the original lockowner that it got from decoding the args. Switch them both to just call locks_release_private() on &argp->lock instead, which should handle the case properly where the info in argp->lock has already been released.

With that changed, we can also eliminate the call to locks_release_private() in nlmsvc_testlock() since the callers will now handle that.

---
 fs/lockd/clntproc.c | 3 +++
 fs/lockd/svc4proc.c | 4 +---
 fs/lockd/svclock.c  | 1 -
 fs/lockd/svcproc.c  | 5 +----
 4 files changed, 5 insertions(+), 8 deletions(-)
fs/lockd/clntproc.c +3 -0
index cebcc283b7ce..d3dd04137677 100644
@@ -438,6 +438,9 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
        if (status < 0)
                goto out;
 
+       /* Release any references held by fl before copying in conflock info */
+       locks_release_private(fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;
fs/lockd/svc4proc.c +1 -3
index 109e5caae8c7..cbfec12296a4 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
        /* Now check for conflicting locks */
        resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
                                       &resp->lock);
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        else
                dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;
fs/lockd/svclock.c +0 -1
index a31dc9588eb8..7ff3d75a5ca3 100644
@@ -652,7 +652,6 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
        conflock->fl.c.flc_type = lock->fl.c.flc_type;
        conflock->fl.fl_start = lock->fl.fl_start;
        conflock->fl.fl_end = lock->fl.fl_end;
-       locks_release_private(&lock->fl);
 
        ret = nlm_lck_denied;
 out:
fs/lockd/svcproc.c +0 -3
index f53d5177f267..6f3e669a3532 100644
@@ -117,7 +117,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST          called\n");
@@ -127,8 +126,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
-
        /* Now check for conflicting locks */
        resp->status = cast_status(nlmsvc_testlock(rqstp, file, host,
                                                   &argp->lock, &resp->lock));

fs/lockd/svcproc.c __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp) · line 141

Quoted 8 lines
fs/lockd/svcproc.c +1 -1
@@ -138,7 +135,7 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
                dprintk("lockd: TEST          status %d vers %d\n",
                        ntohl(resp->status), rqstp->rq_vers);
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;

Results in:

[  420.172241] Modules linked in: ext4 crc16 mbcache jbd2 rpcrdma

rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo videobuf2_vmalloc snd_hda_codec_generic videobuf2_memops uvc videobuf2_v4l2 videobuf2_common snd_hda_intel videodev snd_intel_dspcfg snd_hda_codec mc snd_hda_core snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf sr_mod cdrom e1000e vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink

[  420.176903] CPU: 2 UID: 0 PID: 6338 Comm: lockd Kdump: loaded Not
tainted 6.18.0-rc1+ #40 PREEMPT(voluntary)
[  420.177437] Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS
VMW201.00V.24006586.BA64.2406042154 06/04/2024
[  420.177994] pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
[  420.178357] pc : nlmclnt_proc+0x1bc/0x3a0 [lockd]
[  420.178627] lr : nlmclnt_proc+0x2b8/0x3a0 [lockd]
[  420.179254] sp : ffff800088da7890
[  420.179449] x29: ffff800088da7890 x28: 0000000000000004 x27: 1fffe00013a1ba2e
[  420.179848] x26: ffff0000aad57970 x25: 1fffe000155aaf2e x24: ffff80007fa835a0
[  420.180262] x23: 0000000000000000 x22: 0000000000000005 x21: 0000000000000000
[  420.180656] x20: 0000000000000000 x19: ffff0000aad578e0 x18: 0000000000000a10
[  420.181084] x17: 000000a400000000 x16: 0000000000000000 x15: ffff80007ea17118
[  420.181521] x14: ffff80007fa49358 x13: ffff80007ea526c0 x12: ffff700010be8955
[  420.181907] x11: 1ffff00010be8954 x10: ffff700010be8954 x9 : dfff800000000000
[  420.182314] x8 : 00008fffef4176ac x7 : ffff800085f44aa0 x6 : 0000000000000001
[  420.182721] x5 : ffff800085f44aa0 x4 : ffff0000937d8600 x3 : ffff8001fae51000
[  420.183134] x2 : 0000000000000001 x1 : dfff800000000000 x0 : 0000000000000008
[  420.183519] Call trace:
[  420.183676]  nlmclnt_proc+0x1bc/0x3a0 [lockd] (P)
[  420.183936]  nfs3_proc_lock+0xd4/0x250 [nfsv3]
[  420.184547]  nfs_lock+0x50c/0x740 [nfs]
[  420.186168]  vfs_test_lock+0x8c/0x100
[  420.186675]  nlmsvc_testlock+0xf0/0x640 [lockd]
[  420.187281]  __nlm4svc_proc_test+0x258/0x390 [lockd]
[  420.187596]  nlm4svc_proc_test+0x40/0x60 [lockd]
[  420.187842]  nlmsvc_dispatch+0xb0/0x200 [lockd]
[  420.188084]  svc_process_common+0xb20/0x17b0 [sunrpc]
[  420.188465]  svc_process+0x414/0x900 [sunrpc]
[  420.188714]  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
[  420.189003]  svc_recv+0x1a4/0x520 [sunrpc]
[  420.189247]  lockd+0x154/0x298 [lockd]
[  420.189471]  kthread+0x2f8/0x398
[  420.189653]  ret_from_fork+0x10/0x20
[  420.189852] Code: d2d00001 f2fbffe1 91002280 d343fc02 (38e16841)
[  420.190210] SMP: stopping secondary CPUs
[  420.191613] Starting crashdump kernel...
[  420.191863] Bye!
Quoted 3 lines

--
2.51.0

maintainer
#10 lore raw
Quoted 481 lines · Olga Kornievskaia
Quoted 425 lines · Jeff Layton
Quoted 289 lines · Jeff Layton
Quoted 288 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 656

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +0 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
Quoted 10 lines
Quoted 5 lines
fs/lockd/clntproc.c +2 -0
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;

Sorry, I take it back. That alone won't fix it because of the pointer- saving shenanigans in __nlm4svc_proc_test. We'll need to do this a bit more carefully, I think.

How about this patch? The changelog still needs some work, but I think this is the most correct way to handle it:

-------------------------8<----------------------------

[PATCH] nlm: fix handling of conflocks by NLM

The handling of conflocks can result in a refcount overput on the flc_owner in the case of a reexported NFSv3 fs.

lockd will pass down a file_lock structure in its arguments with flc_owner set to an NLM owner and a valid reference. Once that gets down to the reexported NFS client, it will ignore that field and leave it intact when copying in conflock info.

The NLM server code will then end up releasing the conflock, and then releasing the one from the arguments, not realizing that the lock has already been released.

The owner info in the arguments to vfs_test_lock() is there to give information about the requestor. Once nlmclnt_test() is going to copy in the conflock info however, it needs to release the old owner.

Have nlmclnt_test() do call locks_release_private() after testing the lock to release any info that was in the old file_lock structure.

This creates another problem: __nlm4svc_proc_test() and __nlmsvc_proc_test() both call nlmsvc_put_lockowner() unconditionally on the original lockowner that it got from decoding the args. Switch them both to just call locks_release_private() on &argp->lock instead, which should handle the case properly where the info in argp->lock has already been released.

With that changed, we can also eliminate the call to locks_release_private() in nlmsvc_testlock() since the callers will now handle that.

---
 fs/lockd/clntproc.c | 3 +++
 fs/lockd/svc4proc.c | 4 +---
 fs/lockd/svclock.c  | 1 -
 fs/lockd/svcproc.c  | 5 +----
 4 files changed, 5 insertions(+), 8 deletions(-)
fs/lockd/clntproc.c +3 -0
index cebcc283b7ce..d3dd04137677 100644
@@ -438,6 +438,9 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
        if (status < 0)
                goto out;
 
+       /* Release any references held by fl before copying in conflock info */
+       locks_release_private(fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;
fs/lockd/svc4proc.c +1 -3
index 109e5caae8c7..cbfec12296a4 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
        /* Now check for conflicting locks */
        resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
                                       &resp->lock);
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        else
                dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;
fs/lockd/svclock.c +0 -1
index a31dc9588eb8..7ff3d75a5ca3 100644
@@ -652,7 +652,6 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
        conflock->fl.c.flc_type = lock->fl.c.flc_type;
        conflock->fl.fl_start = lock->fl.fl_start;
        conflock->fl.fl_end = lock->fl.fl_end;
-       locks_release_private(&lock->fl);
 
        ret = nlm_lck_denied;
 out:
fs/lockd/svcproc.c +0 -3
index f53d5177f267..6f3e669a3532 100644
@@ -117,7 +117,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST          called\n");
@@ -127,8 +126,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
-
        /* Now check for conflicting locks */
        resp->status = cast_status(nlmsvc_testlock(rqstp, file, host,
                                                   &argp->lock, &resp->lock));

fs/lockd/svcproc.c __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp) · line 141

Quoted 8 lines
fs/lockd/svcproc.c +1 -1
@@ -138,7 +135,7 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
                dprintk("lockd: TEST          status %d vers %d\n",
                        ntohl(resp->status), rqstp->rq_vers);
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;

Results in:

[  420.172241] Modules linked in: ext4 crc16 mbcache jbd2 rpcrdma

rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo videobuf2_vmalloc snd_hda_codec_generic videobuf2_memops uvc videobuf2_v4l2 videobuf2_common snd_hda_intel videodev snd_intel_dspcfg snd_hda_codec mc snd_hda_core snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf sr_mod cdrom e1000e vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink

[  420.176903] CPU: 2 UID: 0 PID: 6338 Comm: lockd Kdump: loaded Not
tainted 6.18.0-rc1+ #40 PREEMPT(voluntary)
[  420.177437] Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS
VMW201.00V.24006586.BA64.2406042154 06/04/2024
[  420.177994] pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
[  420.178357] pc : nlmclnt_proc+0x1bc/0x3a0 [lockd]
[  420.178627] lr : nlmclnt_proc+0x2b8/0x3a0 [lockd]
[  420.179254] sp : ffff800088da7890
[  420.179449] x29: ffff800088da7890 x28: 0000000000000004 x27: 1fffe00013a1ba2e
[  420.179848] x26: ffff0000aad57970 x25: 1fffe000155aaf2e x24: ffff80007fa835a0
[  420.180262] x23: 0000000000000000 x22: 0000000000000005 x21: 0000000000000000
[  420.180656] x20: 0000000000000000 x19: ffff0000aad578e0 x18: 0000000000000a10
[  420.181084] x17: 000000a400000000 x16: 0000000000000000 x15: ffff80007ea17118
[  420.181521] x14: ffff80007fa49358 x13: ffff80007ea526c0 x12: ffff700010be8955
[  420.181907] x11: 1ffff00010be8954 x10: ffff700010be8954 x9 : dfff800000000000
[  420.182314] x8 : 00008fffef4176ac x7 : ffff800085f44aa0 x6 : 0000000000000001
[  420.182721] x5 : ffff800085f44aa0 x4 : ffff0000937d8600 x3 : ffff8001fae51000
[  420.183134] x2 : 0000000000000001 x1 : dfff800000000000 x0 : 0000000000000008
[  420.183519] Call trace:
[  420.183676]  nlmclnt_proc+0x1bc/0x3a0 [lockd] (P)
[  420.183936]  nfs3_proc_lock+0xd4/0x250 [nfsv3]
[  420.184547]  nfs_lock+0x50c/0x740 [nfs]
[  420.186168]  vfs_test_lock+0x8c/0x100
[  420.186675]  nlmsvc_testlock+0xf0/0x640 [lockd]
[  420.187281]  __nlm4svc_proc_test+0x258/0x390 [lockd]
[  420.187596]  nlm4svc_proc_test+0x40/0x60 [lockd]
[  420.187842]  nlmsvc_dispatch+0xb0/0x200 [lockd]
[  420.188084]  svc_process_common+0xb20/0x17b0 [sunrpc]
[  420.188465]  svc_process+0x414/0x900 [sunrpc]
[  420.188714]  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
Quoted 12 lines
[  420.189003]  svc_recv+0x1a4/0x520 [sunrpc]
[  420.189247]  lockd+0x154/0x298 [lockd]
[  420.189471]  kthread+0x2f8/0x398
[  420.189653]  ret_from_fork+0x10/0x20
[  420.189852] Code: d2d00001 f2fbffe1 91002280 d343fc02 (38e16841)
[  420.190210] SMP: stopping secondary CPUs
[  420.191613] Starting crashdump kernel...
[  420.191863] Bye!
Quoted 3 lines

--
2.51.0

I can't tell much from that stack trace. What happened? Was it a refcounting issue or did it crash someplace else?

Signature · 1 line
Jeff Layton <[email protected]>
#11 lore raw
Quoted 492 lines · Jeff Layton
Quoted 490 lines · Olga Kornievskaia
Quoted 425 lines · Jeff Layton
Quoted 289 lines · Jeff Layton
Quoted 288 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 656

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +0 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
Quoted 10 lines
Quoted 5 lines
fs/lockd/clntproc.c +2 -0
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;

Sorry, I take it back. That alone won't fix it because of the pointer- saving shenanigans in __nlm4svc_proc_test. We'll need to do this a bit more carefully, I think.

How about this patch? The changelog still needs some work, but I think this is the most correct way to handle it:

-------------------------8<----------------------------

[PATCH] nlm: fix handling of conflocks by NLM

The handling of conflocks can result in a refcount overput on the flc_owner in the case of a reexported NFSv3 fs.

lockd will pass down a file_lock structure in its arguments with flc_owner set to an NLM owner and a valid reference. Once that gets down to the reexported NFS client, it will ignore that field and leave it intact when copying in conflock info.

The NLM server code will then end up releasing the conflock, and then releasing the one from the arguments, not realizing that the lock has already been released.

The owner info in the arguments to vfs_test_lock() is there to give information about the requestor. Once nlmclnt_test() is going to copy in the conflock info however, it needs to release the old owner.

Have nlmclnt_test() do call locks_release_private() after testing the lock to release any info that was in the old file_lock structure.

This creates another problem: __nlm4svc_proc_test() and __nlmsvc_proc_test() both call nlmsvc_put_lockowner() unconditionally on the original lockowner that it got from decoding the args. Switch them both to just call locks_release_private() on &argp->lock instead, which should handle the case properly where the info in argp->lock has already been released.

With that changed, we can also eliminate the call to locks_release_private() in nlmsvc_testlock() since the callers will now handle that.

---
 fs/lockd/clntproc.c | 3 +++
 fs/lockd/svc4proc.c | 4 +---
 fs/lockd/svclock.c  | 1 -
 fs/lockd/svcproc.c  | 5 +----
 4 files changed, 5 insertions(+), 8 deletions(-)
fs/lockd/clntproc.c +3 -0
index cebcc283b7ce..d3dd04137677 100644
@@ -438,6 +438,9 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
        if (status < 0)
                goto out;
 
+       /* Release any references held by fl before copying in conflock info */
+       locks_release_private(fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;
fs/lockd/svc4proc.c +1 -3
index 109e5caae8c7..cbfec12296a4 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
        /* Now check for conflicting locks */
        resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
                                       &resp->lock);
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        else
                dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;
fs/lockd/svclock.c +0 -1
index a31dc9588eb8..7ff3d75a5ca3 100644
@@ -652,7 +652,6 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
        conflock->fl.c.flc_type = lock->fl.c.flc_type;
        conflock->fl.fl_start = lock->fl.fl_start;
        conflock->fl.fl_end = lock->fl.fl_end;
-       locks_release_private(&lock->fl);
 
        ret = nlm_lck_denied;
 out:
fs/lockd/svcproc.c +0 -3
index f53d5177f267..6f3e669a3532 100644
@@ -117,7 +117,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST          called\n");
@@ -127,8 +126,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
-
        /* Now check for conflicting locks */
        resp->status = cast_status(nlmsvc_testlock(rqstp, file, host,
                                                   &argp->lock, &resp->lock));

fs/lockd/svcproc.c __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp) · line 141

Quoted 8 lines
fs/lockd/svcproc.c +1 -1
@@ -138,7 +135,7 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
                dprintk("lockd: TEST          status %d vers %d\n",
                        ntohl(resp->status), rqstp->rq_vers);
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;

Results in:

[  420.172241] Modules linked in: ext4 crc16 mbcache jbd2 rpcrdma

rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo videobuf2_vmalloc snd_hda_codec_generic videobuf2_memops uvc videobuf2_v4l2 videobuf2_common snd_hda_intel videodev snd_intel_dspcfg snd_hda_codec mc snd_hda_core snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf sr_mod cdrom e1000e vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink

[  420.176903] CPU: 2 UID: 0 PID: 6338 Comm: lockd Kdump: loaded Not
tainted 6.18.0-rc1+ #40 PREEMPT(voluntary)
[  420.177437] Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS
VMW201.00V.24006586.BA64.2406042154 06/04/2024
[  420.177994] pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
[  420.178357] pc : nlmclnt_proc+0x1bc/0x3a0 [lockd]
[  420.178627] lr : nlmclnt_proc+0x2b8/0x3a0 [lockd]
[  420.179254] sp : ffff800088da7890
[  420.179449] x29: ffff800088da7890 x28: 0000000000000004 x27: 1fffe00013a1ba2e
[  420.179848] x26: ffff0000aad57970 x25: 1fffe000155aaf2e x24: ffff80007fa835a0
[  420.180262] x23: 0000000000000000 x22: 0000000000000005 x21: 0000000000000000
[  420.180656] x20: 0000000000000000 x19: ffff0000aad578e0 x18: 0000000000000a10
[  420.181084] x17: 000000a400000000 x16: 0000000000000000 x15: ffff80007ea17118
[  420.181521] x14: ffff80007fa49358 x13: ffff80007ea526c0 x12: ffff700010be8955
[  420.181907] x11: 1ffff00010be8954 x10: ffff700010be8954 x9 : dfff800000000000
[  420.182314] x8 : 00008fffef4176ac x7 : ffff800085f44aa0 x6 : 0000000000000001
[  420.182721] x5 : ffff800085f44aa0 x4 : ffff0000937d8600 x3 : ffff8001fae51000
[  420.183134] x2 : 0000000000000001 x1 : dfff800000000000 x0 : 0000000000000008
[  420.183519] Call trace:
[  420.183676]  nlmclnt_proc+0x1bc/0x3a0 [lockd] (P)
[  420.183936]  nfs3_proc_lock+0xd4/0x250 [nfsv3]
[  420.184547]  nfs_lock+0x50c/0x740 [nfs]
[  420.186168]  vfs_test_lock+0x8c/0x100
[  420.186675]  nlmsvc_testlock+0xf0/0x640 [lockd]
[  420.187281]  __nlm4svc_proc_test+0x258/0x390 [lockd]
[  420.187596]  nlm4svc_proc_test+0x40/0x60 [lockd]
[  420.187842]  nlmsvc_dispatch+0xb0/0x200 [lockd]
[  420.188084]  svc_process_common+0xb20/0x17b0 [sunrpc]
[  420.188465]  svc_process+0x414/0x900 [sunrpc]
[  420.188714]  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
[  420.189003]  svc_recv+0x1a4/0x520 [sunrpc]
[  420.189247]  lockd+0x154/0x298 [lockd]
[  420.189471]  kthread+0x2f8/0x398
[  420.189653]  ret_from_fork+0x10/0x20
[  420.189852] Code: d2d00001 f2fbffe1 91002280 d343fc02 (38e16841)
[  420.190210] SMP: stopping secondary CPUs
[  420.191613] Starting crashdump kernel...
[  420.191863] Bye!
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

--
2.51.0

I can't tell much from that stack trace. What happened? Was it a refcounting issue or did it crash someplace else?

It crashed while trying to process the TEST lock request (coming from the nfs client) but crashed in a different place than before....

(gdb) l *(nlmclnt_proc+0x1bc)
0x5f04 is in nlmclnt_proc (fs/lockd/clntproc.c:195). 190 status = nlmclnt_unlock(call, fl);
191 } else if (IS_GETLK(cmd))
192 status = nlmclnt_test(call, fl);
193 else
194 status = -EINVAL;
195 fl->fl_ops->fl_release_private(fl);
196 fl->fl_ops = NULL;
197
198 dprintk("lockd: clnt proc returns %d\n", status); 199 return status;

So I'm not 100% certain but the patch added lock_release_private() to the nlmclnt_test(). So in nlmclnt_proc() didn't we then already call it and release private in line 192 and then we are trying to do it again in line 195?
locks_release_private() set's fl->fl_ops to NULL.... I'm speculating that's the reason for the oops

Quoted 3 lines

--
Jeff Layton <[email protected]>

maintainer
#12 lore raw
Quoted 503 lines · Olga Kornievskaia
Quoted 492 lines · Jeff Layton
Quoted 490 lines · Olga Kornievskaia
Quoted 425 lines · Jeff Layton
Quoted 289 lines · Jeff Layton
Quoted 288 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 656

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +0 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
Quoted 10 lines
Quoted 5 lines
fs/lockd/clntproc.c +2 -0
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;

Sorry, I take it back. That alone won't fix it because of the pointer- saving shenanigans in __nlm4svc_proc_test. We'll need to do this a bit more carefully, I think.

How about this patch? The changelog still needs some work, but I think this is the most correct way to handle it:

-------------------------8<----------------------------

[PATCH] nlm: fix handling of conflocks by NLM

The handling of conflocks can result in a refcount overput on the flc_owner in the case of a reexported NFSv3 fs.

lockd will pass down a file_lock structure in its arguments with flc_owner set to an NLM owner and a valid reference. Once that gets down to the reexported NFS client, it will ignore that field and leave it intact when copying in conflock info.

The NLM server code will then end up releasing the conflock, and then releasing the one from the arguments, not realizing that the lock has already been released.

The owner info in the arguments to vfs_test_lock() is there to give information about the requestor. Once nlmclnt_test() is going to copy in the conflock info however, it needs to release the old owner.

Have nlmclnt_test() do call locks_release_private() after testing the lock to release any info that was in the old file_lock structure.

This creates another problem: __nlm4svc_proc_test() and __nlmsvc_proc_test() both call nlmsvc_put_lockowner() unconditionally on the original lockowner that it got from decoding the args. Switch them both to just call locks_release_private() on &argp->lock instead, which should handle the case properly where the info in argp->lock has already been released.

With that changed, we can also eliminate the call to locks_release_private() in nlmsvc_testlock() since the callers will now handle that.

---
 fs/lockd/clntproc.c | 3 +++
 fs/lockd/svc4proc.c | 4 +---
 fs/lockd/svclock.c  | 1 -
 fs/lockd/svcproc.c  | 5 +----
 4 files changed, 5 insertions(+), 8 deletions(-)
fs/lockd/clntproc.c +3 -0
index cebcc283b7ce..d3dd04137677 100644
@@ -438,6 +438,9 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
        if (status < 0)
                goto out;
 
+       /* Release any references held by fl before copying in conflock info */
+       locks_release_private(fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;
fs/lockd/svc4proc.c +1 -3
index 109e5caae8c7..cbfec12296a4 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
        /* Now check for conflicting locks */
        resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
                                       &resp->lock);
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        else
                dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;
fs/lockd/svclock.c +0 -1
index a31dc9588eb8..7ff3d75a5ca3 100644
@@ -652,7 +652,6 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
        conflock->fl.c.flc_type = lock->fl.c.flc_type;
        conflock->fl.fl_start = lock->fl.fl_start;
        conflock->fl.fl_end = lock->fl.fl_end;
-       locks_release_private(&lock->fl);
 
        ret = nlm_lck_denied;
 out:
fs/lockd/svcproc.c +0 -3
index f53d5177f267..6f3e669a3532 100644
@@ -117,7 +117,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST          called\n");
@@ -127,8 +126,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
-
        /* Now check for conflicting locks */
        resp->status = cast_status(nlmsvc_testlock(rqstp, file, host,
                                                   &argp->lock, &resp->lock));

fs/lockd/svcproc.c __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp) · line 141

Quoted 8 lines
fs/lockd/svcproc.c +1 -1
@@ -138,7 +135,7 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
                dprintk("lockd: TEST          status %d vers %d\n",
                        ntohl(resp->status), rqstp->rq_vers);
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;

Results in:

[  420.172241] Modules linked in: ext4 crc16 mbcache jbd2 rpcrdma

rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo videobuf2_vmalloc snd_hda_codec_generic videobuf2_memops uvc videobuf2_v4l2 videobuf2_common snd_hda_intel videodev snd_intel_dspcfg snd_hda_codec mc snd_hda_core snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf sr_mod cdrom e1000e vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink

[  420.176903] CPU: 2 UID: 0 PID: 6338 Comm: lockd Kdump: loaded Not
tainted 6.18.0-rc1+ #40 PREEMPT(voluntary)
[  420.177437] Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS
VMW201.00V.24006586.BA64.2406042154 06/04/2024
[  420.177994] pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
[  420.178357] pc : nlmclnt_proc+0x1bc/0x3a0 [lockd]
[  420.178627] lr : nlmclnt_proc+0x2b8/0x3a0 [lockd]
[  420.179254] sp : ffff800088da7890
[  420.179449] x29: ffff800088da7890 x28: 0000000000000004 x27: 1fffe00013a1ba2e
[  420.179848] x26: ffff0000aad57970 x25: 1fffe000155aaf2e x24: ffff80007fa835a0
[  420.180262] x23: 0000000000000000 x22: 0000000000000005 x21: 0000000000000000
[  420.180656] x20: 0000000000000000 x19: ffff0000aad578e0 x18: 0000000000000a10
[  420.181084] x17: 000000a400000000 x16: 0000000000000000 x15: ffff80007ea17118
[  420.181521] x14: ffff80007fa49358 x13: ffff80007ea526c0 x12: ffff700010be8955
[  420.181907] x11: 1ffff00010be8954 x10: ffff700010be8954 x9 : dfff800000000000
[  420.182314] x8 : 00008fffef4176ac x7 : ffff800085f44aa0 x6 : 0000000000000001
[  420.182721] x5 : ffff800085f44aa0 x4 : ffff0000937d8600 x3 : ffff8001fae51000
[  420.183134] x2 : 0000000000000001 x1 : dfff800000000000 x0 : 0000000000000008
[  420.183519] Call trace:
[  420.183676]  nlmclnt_proc+0x1bc/0x3a0 [lockd] (P)
[  420.183936]  nfs3_proc_lock+0xd4/0x250 [nfsv3]
[  420.184547]  nfs_lock+0x50c/0x740 [nfs]
[  420.186168]  vfs_test_lock+0x8c/0x100
[  420.186675]  nlmsvc_testlock+0xf0/0x640 [lockd]
[  420.187281]  __nlm4svc_proc_test+0x258/0x390 [lockd]
[  420.187596]  nlm4svc_proc_test+0x40/0x60 [lockd]
[  420.187842]  nlmsvc_dispatch+0xb0/0x200 [lockd]
[  420.188084]  svc_process_common+0xb20/0x17b0 [sunrpc]
[  420.188465]  svc_process+0x414/0x900 [sunrpc]
[  420.188714]  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
[  420.189003]  svc_recv+0x1a4/0x520 [sunrpc]
[  420.189247]  lockd+0x154/0x298 [lockd]
[  420.189471]  kthread+0x2f8/0x398
[  420.189653]  ret_from_fork+0x10/0x20
[  420.189852] Code: d2d00001 f2fbffe1 91002280 d343fc02 (38e16841)
[  420.190210] SMP: stopping secondary CPUs
[  420.191613] Starting crashdump kernel...
[  420.191863] Bye!
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

--
2.51.0

I can't tell much from that stack trace. What happened? Was it a refcounting issue or did it crash someplace else?

It crashed while trying to process the TEST lock request (coming from the nfs client) but crashed in a different place than before....

Quoted 9 lines

(gdb) l *(nlmclnt_proc+0x1bc)
0x5f04 is in nlmclnt_proc (fs/lockd/clntproc.c:195). 190 status = nlmclnt_unlock(call, fl);
191 } else if (IS_GETLK(cmd))
192 status = nlmclnt_test(call, fl);
193 else
194 status = -EINVAL;
195 fl->fl_ops->fl_release_private(fl);
196 fl->fl_ops = NULL;

Let's turn lines 195 and 196 into a call to locks_release_private(fl). If you do that, does it work?

Quoted 14 lines

197
198 dprintk("lockd: clnt proc returns %d\n", status); 199 return status;

So I'm not 100% certain but the patch added lock_release_private() to the nlmclnt_test(). So in nlmclnt_proc() didn't we then already call it and release private in line 192 and then we are trying to do it again in line 195?
locks_release_private() set's fl->fl_ops to NULL.... I'm speculating that's the reason for the oops

Quoted 3 lines

--
Jeff Layton <[email protected]>

Signature · 1 line
Jeff Layton <[email protected]>
#13 lore raw
Quoted 516 lines · Jeff Layton
Quoted 503 lines · Olga Kornievskaia
Quoted 492 lines · Jeff Layton
Quoted 490 lines · Olga Kornievskaia
Quoted 425 lines · Jeff Layton
Quoted 289 lines · Jeff Layton
Quoted 288 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 656

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +0 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
Quoted 10 lines
Quoted 5 lines
fs/lockd/clntproc.c +2 -0
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;

Sorry, I take it back. That alone won't fix it because of the pointer- saving shenanigans in __nlm4svc_proc_test. We'll need to do this a bit more carefully, I think.

How about this patch? The changelog still needs some work, but I think this is the most correct way to handle it:

-------------------------8<----------------------------

[PATCH] nlm: fix handling of conflocks by NLM

The handling of conflocks can result in a refcount overput on the flc_owner in the case of a reexported NFSv3 fs.

lockd will pass down a file_lock structure in its arguments with flc_owner set to an NLM owner and a valid reference. Once that gets down to the reexported NFS client, it will ignore that field and leave it intact when copying in conflock info.

The NLM server code will then end up releasing the conflock, and then releasing the one from the arguments, not realizing that the lock has already been released.

The owner info in the arguments to vfs_test_lock() is there to give information about the requestor. Once nlmclnt_test() is going to copy in the conflock info however, it needs to release the old owner.

Have nlmclnt_test() do call locks_release_private() after testing the lock to release any info that was in the old file_lock structure.

This creates another problem: __nlm4svc_proc_test() and __nlmsvc_proc_test() both call nlmsvc_put_lockowner() unconditionally on the original lockowner that it got from decoding the args. Switch them both to just call locks_release_private() on &argp->lock instead, which should handle the case properly where the info in argp->lock has already been released.

With that changed, we can also eliminate the call to locks_release_private() in nlmsvc_testlock() since the callers will now handle that.

---
 fs/lockd/clntproc.c | 3 +++
 fs/lockd/svc4proc.c | 4 +---
 fs/lockd/svclock.c  | 1 -
 fs/lockd/svcproc.c  | 5 +----
 4 files changed, 5 insertions(+), 8 deletions(-)
fs/lockd/clntproc.c +3 -0
index cebcc283b7ce..d3dd04137677 100644
@@ -438,6 +438,9 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
        if (status < 0)
                goto out;
 
+       /* Release any references held by fl before copying in conflock info */
+       locks_release_private(fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;
fs/lockd/svc4proc.c +1 -3
index 109e5caae8c7..cbfec12296a4 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
        /* Now check for conflicting locks */
        resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
                                       &resp->lock);
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        else
                dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;
fs/lockd/svclock.c +0 -1
index a31dc9588eb8..7ff3d75a5ca3 100644
@@ -652,7 +652,6 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
        conflock->fl.c.flc_type = lock->fl.c.flc_type;
        conflock->fl.fl_start = lock->fl.fl_start;
        conflock->fl.fl_end = lock->fl.fl_end;
-       locks_release_private(&lock->fl);
 
        ret = nlm_lck_denied;
 out:
fs/lockd/svcproc.c +0 -3
index f53d5177f267..6f3e669a3532 100644
@@ -117,7 +117,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST          called\n");
@@ -127,8 +126,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
-
        /* Now check for conflicting locks */
        resp->status = cast_status(nlmsvc_testlock(rqstp, file, host,
                                                   &argp->lock, &resp->lock));

fs/lockd/svcproc.c __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp) · line 141

Quoted 8 lines
fs/lockd/svcproc.c +1 -1
@@ -138,7 +135,7 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
                dprintk("lockd: TEST          status %d vers %d\n",
                        ntohl(resp->status), rqstp->rq_vers);
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;

Results in:

[  420.172241] Modules linked in: ext4 crc16 mbcache jbd2 rpcrdma

rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo videobuf2_vmalloc snd_hda_codec_generic videobuf2_memops uvc videobuf2_v4l2 videobuf2_common snd_hda_intel videodev snd_intel_dspcfg snd_hda_codec mc snd_hda_core snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf sr_mod cdrom e1000e vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink

[  420.176903] CPU: 2 UID: 0 PID: 6338 Comm: lockd Kdump: loaded Not
tainted 6.18.0-rc1+ #40 PREEMPT(voluntary)
[  420.177437] Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS
VMW201.00V.24006586.BA64.2406042154 06/04/2024
[  420.177994] pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
[  420.178357] pc : nlmclnt_proc+0x1bc/0x3a0 [lockd]
[  420.178627] lr : nlmclnt_proc+0x2b8/0x3a0 [lockd]
[  420.179254] sp : ffff800088da7890
[  420.179449] x29: ffff800088da7890 x28: 0000000000000004 x27: 1fffe00013a1ba2e
[  420.179848] x26: ffff0000aad57970 x25: 1fffe000155aaf2e x24: ffff80007fa835a0
[  420.180262] x23: 0000000000000000 x22: 0000000000000005 x21: 0000000000000000
[  420.180656] x20: 0000000000000000 x19: ffff0000aad578e0 x18: 0000000000000a10
[  420.181084] x17: 000000a400000000 x16: 0000000000000000 x15: ffff80007ea17118
[  420.181521] x14: ffff80007fa49358 x13: ffff80007ea526c0 x12: ffff700010be8955
[  420.181907] x11: 1ffff00010be8954 x10: ffff700010be8954 x9 : dfff800000000000
[  420.182314] x8 : 00008fffef4176ac x7 : ffff800085f44aa0 x6 : 0000000000000001
[  420.182721] x5 : ffff800085f44aa0 x4 : ffff0000937d8600 x3 : ffff8001fae51000
[  420.183134] x2 : 0000000000000001 x1 : dfff800000000000 x0 : 0000000000000008
[  420.183519] Call trace:
[  420.183676]  nlmclnt_proc+0x1bc/0x3a0 [lockd] (P)
[  420.183936]  nfs3_proc_lock+0xd4/0x250 [nfsv3]
[  420.184547]  nfs_lock+0x50c/0x740 [nfs]
[  420.186168]  vfs_test_lock+0x8c/0x100
[  420.186675]  nlmsvc_testlock+0xf0/0x640 [lockd]
[  420.187281]  __nlm4svc_proc_test+0x258/0x390 [lockd]
[  420.187596]  nlm4svc_proc_test+0x40/0x60 [lockd]
[  420.187842]  nlmsvc_dispatch+0xb0/0x200 [lockd]
[  420.188084]  svc_process_common+0xb20/0x17b0 [sunrpc]
[  420.188465]  svc_process+0x414/0x900 [sunrpc]
[  420.188714]  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
[  420.189003]  svc_recv+0x1a4/0x520 [sunrpc]
[  420.189247]  lockd+0x154/0x298 [lockd]
[  420.189471]  kthread+0x2f8/0x398
[  420.189653]  ret_from_fork+0x10/0x20
[  420.189852] Code: d2d00001 f2fbffe1 91002280 d343fc02 (38e16841)
[  420.190210] SMP: stopping secondary CPUs
[  420.191613] Starting crashdump kernel...
[  420.191863] Bye!
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

--
2.51.0

I can't tell much from that stack trace. What happened? Was it a refcounting issue or did it crash someplace else?

It crashed while trying to process the TEST lock request (coming from the nfs client) but crashed in a different place than before....

Quoted 9 lines

(gdb) l *(nlmclnt_proc+0x1bc)
0x5f04 is in nlmclnt_proc (fs/lockd/clntproc.c:195). 190 status = nlmclnt_unlock(call, fl);
191 } else if (IS_GETLK(cmd))
192 status = nlmclnt_test(call, fl);
193 else
194 status = -EINVAL;
195 fl->fl_ops->fl_release_private(fl);
196 fl->fl_ops = NULL;

Quoted 2 lines

Let's turn lines 195 and 196 into a call to locks_release_private(fl). If you do that, does it work?

No crash.

Quoted 17 lines
Quoted 4 lines

197
198 dprintk("lockd: clnt proc returns %d\n", status); 199 return status;

Quoted 10 lines

So I'm not 100% certain but the patch added lock_release_private() to the nlmclnt_test(). So in nlmclnt_proc() didn't we then already call it and release private in line 192 and then we are trying to do it again in line 195?
locks_release_private() set's fl->fl_ops to NULL.... I'm speculating that's the reason for the oops

Quoted 3 lines

--
Jeff Layton <[email protected]>

--
Jeff Layton <[email protected]>

#14 lore raw
In reply to Jeff Layton
Quoted 383 lines · Jeff Layton
Quoted 289 lines · Jeff Layton
Quoted 288 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 656

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +0 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
Quoted 10 lines
Quoted 5 lines
fs/lockd/clntproc.c +2 -0
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;

Sorry, I take it back. That alone won't fix it because of the pointer- saving shenanigans in __nlm4svc_proc_test. We'll need to do this a bit more carefully, I think.

How about this patch? The changelog still needs some work, but I think this is the most correct way to handle it:

-------------------------8<----------------------------

[PATCH] nlm: fix handling of conflocks by NLM

The handling of conflocks can result in a refcount overput on the flc_owner in the case of a reexported NFSv3 fs.

lockd will pass down a file_lock structure in its arguments with flc_owner set to an NLM owner and a valid reference. Once that gets down to the reexported NFS client, it will ignore that field and leave it intact when copying in conflock info.

The NLM server code will then end up releasing the conflock, and then releasing the one from the arguments, not realizing that the lock has already been released.

The owner info in the arguments to vfs_test_lock() is there to give information about the requestor. Once nlmclnt_test() is going to copy in the conflock info however, it needs to release the old owner.

Have nlmclnt_test() do call locks_release_private() after testing the lock to release any info that was in the old file_lock structure.

This creates another problem: __nlm4svc_proc_test() and __nlmsvc_proc_test() both call nlmsvc_put_lockowner() unconditionally on the original lockowner that it got from decoding the args. Switch them both to just call locks_release_private() on &argp->lock instead, which should handle the case properly where the info in argp->lock has already been released.

With that changed, we can also eliminate the call to locks_release_private() in nlmsvc_testlock() since the callers will now handle that.

---
 fs/lockd/clntproc.c | 3 +++
 fs/lockd/svc4proc.c | 4 +---
 fs/lockd/svclock.c  | 1 -
 fs/lockd/svcproc.c  | 5 +----
 4 files changed, 5 insertions(+), 8 deletions(-)
fs/lockd/clntproc.c +3 -0
index cebcc283b7ce..d3dd04137677 100644
@@ -438,6 +438,9 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
        if (status < 0)
                goto out;
 
+       /* Release any references held by fl before copying in conflock info */
+       locks_release_private(fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;
fs/lockd/svc4proc.c +0 -2
index 109e5caae8c7..cbfec12296a4 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
        /* Now check for conflicting locks */
        resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
                                       &resp->lock);

fs/lockd/svc4proc.c __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp) · line 120

Quoted 8 lines
fs/lockd/svc4proc.c +1 -1
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        else
                dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;

Aren't these svc4proc changes defeat the purpose of the following commit:

commit 184cefbe62627730c30282df12bcff9aae4816ea

Author: Benjamin Coddington <[email protected]>
Date:   Mon Jun 13 09:40:06 2022 -0400

NLM: Defend against file_lock changes after vfs_test_lock()

Instead of trusting that struct file_lock returns completely unchanged after vfs_test_lock() when there's no conflicting lock, stash away our nlm_lockowner reference so we can properly release it for all cases.

This defends against another file_lock implementation overwriting fl_owner when the return type is F_UNLCK.

Not that they are the cause of the crash but I would imagine it would cause a problem fixed by the patch....

Quoted 45 lines
fs/lockd/svclock.c +0 -1
index a31dc9588eb8..7ff3d75a5ca3 100644
@@ -652,7 +652,6 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
        conflock->fl.c.flc_type = lock->fl.c.flc_type;
        conflock->fl.fl_start = lock->fl.fl_start;
        conflock->fl.fl_end = lock->fl.fl_end;
-       locks_release_private(&lock->fl);
 
        ret = nlm_lck_denied;
 out:
fs/lockd/svcproc.c +1 -5
index f53d5177f267..6f3e669a3532 100644
@@ -117,7 +117,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST          called\n");
@@ -127,8 +126,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
-
        /* Now check for conflicting locks */
        resp->status = cast_status(nlmsvc_testlock(rqstp, file, host,
                                                   &argp->lock, &resp->lock));
@@ -138,7 +135,7 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
                dprintk("lockd: TEST          status %d vers %d\n",
                        ntohl(resp->status), rqstp->rq_vers);
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;
--

2.51.0

maintainer
#15 lore raw
Quoted 406 lines · Olga Kornievskaia
Quoted 383 lines · Jeff Layton
Quoted 289 lines · Jeff Layton
Quoted 288 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 656

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +0 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
Quoted 10 lines
Quoted 5 lines
fs/lockd/clntproc.c +2 -0
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;

Sorry, I take it back. That alone won't fix it because of the pointer- saving shenanigans in __nlm4svc_proc_test. We'll need to do this a bit more carefully, I think.

How about this patch? The changelog still needs some work, but I think this is the most correct way to handle it:

-------------------------8<----------------------------

[PATCH] nlm: fix handling of conflocks by NLM

The handling of conflocks can result in a refcount overput on the flc_owner in the case of a reexported NFSv3 fs.

lockd will pass down a file_lock structure in its arguments with flc_owner set to an NLM owner and a valid reference. Once that gets down to the reexported NFS client, it will ignore that field and leave it intact when copying in conflock info.

The NLM server code will then end up releasing the conflock, and then releasing the one from the arguments, not realizing that the lock has already been released.

The owner info in the arguments to vfs_test_lock() is there to give information about the requestor. Once nlmclnt_test() is going to copy in the conflock info however, it needs to release the old owner.

Have nlmclnt_test() do call locks_release_private() after testing the lock to release any info that was in the old file_lock structure.

This creates another problem: __nlm4svc_proc_test() and __nlmsvc_proc_test() both call nlmsvc_put_lockowner() unconditionally on the original lockowner that it got from decoding the args. Switch them both to just call locks_release_private() on &argp->lock instead, which should handle the case properly where the info in argp->lock has already been released.

With that changed, we can also eliminate the call to locks_release_private() in nlmsvc_testlock() since the callers will now handle that.

---
 fs/lockd/clntproc.c | 3 +++
 fs/lockd/svc4proc.c | 4 +---
 fs/lockd/svclock.c  | 1 -
 fs/lockd/svcproc.c  | 5 +----
 4 files changed, 5 insertions(+), 8 deletions(-)
fs/lockd/clntproc.c +3 -0
index cebcc283b7ce..d3dd04137677 100644
@@ -438,6 +438,9 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
        if (status < 0)
                goto out;
 
+       /* Release any references held by fl before copying in conflock info */
+       locks_release_private(fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;
fs/lockd/svc4proc.c +0 -2
index 109e5caae8c7..cbfec12296a4 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
        /* Now check for conflicting locks */
        resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
                                       &resp->lock);

fs/lockd/svc4proc.c __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp) · line 120

Quoted 8 lines
fs/lockd/svc4proc.c +1 -1
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        else
                dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;

Aren't these svc4proc changes defeat the purpose of the following commit:

commit 184cefbe62627730c30282df12bcff9aae4816ea

Author: Benjamin Coddington <[email protected]>
Date:   Mon Jun 13 09:40:06 2022 -0400

NLM: Defend against file_lock changes after vfs_test_lock()

Instead of trusting that struct file_lock returns completely unchanged after vfs_test_lock() when there's no conflicting lock, stash away our nlm_lockowner reference so we can properly release it for all cases.

Quoted 6 lines

This defends against another file_lock implementation overwriting fl_owner when the return type is F_UNLCK.

Not that they are the cause of the crash but I would imagine it would cause a problem fixed by the patch....

Yes, it would. I'm not sure that patch is correct.

Since the flc_owner is refcounted in some cases, it's incumbent on the filesystem's ->lock to properly release those resources in the old file lock before clobbering those fields with the conflock info.

Longer term, I think Neil is right and we probably need to fix vfs_test_lock and the lock inode_operation to take a separate conflock for testlock purposes. That's a bigger change though (particularly the ->lock operations).

Quoted 45 lines
Quoted 45 lines
fs/lockd/svclock.c +0 -1
index a31dc9588eb8..7ff3d75a5ca3 100644
@@ -652,7 +652,6 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
        conflock->fl.c.flc_type = lock->fl.c.flc_type;
        conflock->fl.fl_start = lock->fl.fl_start;
        conflock->fl.fl_end = lock->fl.fl_end;
-       locks_release_private(&lock->fl);
 
        ret = nlm_lck_denied;
 out:
fs/lockd/svcproc.c +1 -5
index f53d5177f267..6f3e669a3532 100644
@@ -117,7 +117,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST          called\n");
@@ -127,8 +126,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
-
        /* Now check for conflicting locks */
        resp->status = cast_status(nlmsvc_testlock(rqstp, file, host,
                                                   &argp->lock, &resp->lock));
@@ -138,7 +135,7 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
                dprintk("lockd: TEST          status %d vers %d\n",
                        ntohl(resp->status), rqstp->rq_vers);
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;
--

2.51.0

Signature · 1 line
Jeff Layton <[email protected]>
#16 lore raw
Quoted 408 lines · Jeff Layton
Quoted 406 lines · Olga Kornievskaia
Quoted 383 lines · Jeff Layton
Quoted 289 lines · Jeff Layton
Quoted 288 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 656

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +0 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
Quoted 10 lines
Quoted 5 lines
fs/lockd/clntproc.c +2 -0
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;

Sorry, I take it back. That alone won't fix it because of the pointer- saving shenanigans in __nlm4svc_proc_test. We'll need to do this a bit more carefully, I think.

How about this patch? The changelog still needs some work, but I think this is the most correct way to handle it:

-------------------------8<----------------------------

[PATCH] nlm: fix handling of conflocks by NLM

The handling of conflocks can result in a refcount overput on the flc_owner in the case of a reexported NFSv3 fs.

lockd will pass down a file_lock structure in its arguments with flc_owner set to an NLM owner and a valid reference. Once that gets down to the reexported NFS client, it will ignore that field and leave it intact when copying in conflock info.

The NLM server code will then end up releasing the conflock, and then releasing the one from the arguments, not realizing that the lock has already been released.

The owner info in the arguments to vfs_test_lock() is there to give information about the requestor. Once nlmclnt_test() is going to copy in the conflock info however, it needs to release the old owner.

Have nlmclnt_test() do call locks_release_private() after testing the lock to release any info that was in the old file_lock structure.

This creates another problem: __nlm4svc_proc_test() and __nlmsvc_proc_test() both call nlmsvc_put_lockowner() unconditionally on the original lockowner that it got from decoding the args. Switch them both to just call locks_release_private() on &argp->lock instead, which should handle the case properly where the info in argp->lock has already been released.

With that changed, we can also eliminate the call to locks_release_private() in nlmsvc_testlock() since the callers will now handle that.

---
 fs/lockd/clntproc.c | 3 +++
 fs/lockd/svc4proc.c | 4 +---
 fs/lockd/svclock.c  | 1 -
 fs/lockd/svcproc.c  | 5 +----
 4 files changed, 5 insertions(+), 8 deletions(-)
fs/lockd/clntproc.c +3 -0
index cebcc283b7ce..d3dd04137677 100644
@@ -438,6 +438,9 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
        if (status < 0)
                goto out;
 
+       /* Release any references held by fl before copying in conflock info */
+       locks_release_private(fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;
fs/lockd/svc4proc.c +0 -2
index 109e5caae8c7..cbfec12296a4 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
        /* Now check for conflicting locks */
        resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
                                       &resp->lock);

fs/lockd/svc4proc.c __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp) · line 120

Quoted 8 lines
fs/lockd/svc4proc.c +1 -1
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        else
                dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;

Aren't these svc4proc changes defeat the purpose of the following commit:

commit 184cefbe62627730c30282df12bcff9aae4816ea

Author: Benjamin Coddington <[email protected]>
Date:   Mon Jun 13 09:40:06 2022 -0400

NLM: Defend against file_lock changes after vfs_test_lock()

Instead of trusting that struct file_lock returns completely unchanged after vfs_test_lock() when there's no conflicting lock, stash away our nlm_lockowner reference so we can properly release it for all cases.

Quoted 8 lines
Quoted 6 lines

This defends against another file_lock implementation overwriting fl_owner when the return type is F_UNLCK.

Not that they are the cause of the crash but I would imagine it would cause a problem fixed by the patch....

Yes, it would. I'm not sure that patch is correct.

But it seems wrong that we are now considering putting a patch that fixes one issue (ie crash on reexport) but re-introduces another issue (that the reverted code fixed be it not in a correct manner).

Quoted 58 lines

Since the flc_owner is refcounted in some cases, it's incumbent on the filesystem's ->lock to properly release those resources in the old file lock before clobbering those fields with the conflock info.

Longer term, I think Neil is right and we probably need to fix vfs_test_lock and the lock inode_operation to take a separate conflock for testlock purposes. That's a bigger change though (particularly the ->lock operations).

Quoted 37 lines
Quoted 37 lines
fs/lockd/svclock.c +0 -1
index a31dc9588eb8..7ff3d75a5ca3 100644
@@ -652,7 +652,6 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
        conflock->fl.c.flc_type = lock->fl.c.flc_type;
        conflock->fl.fl_start = lock->fl.fl_start;
        conflock->fl.fl_end = lock->fl.fl_end;
-       locks_release_private(&lock->fl);
 
        ret = nlm_lck_denied;
 out:
fs/lockd/svcproc.c +0 -3
index f53d5177f267..6f3e669a3532 100644
@@ -117,7 +117,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST          called\n");
@@ -127,8 +126,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
-
        /* Now check for conflicting locks */
        resp->status = cast_status(nlmsvc_testlock(rqstp, file, host,
                                                   &argp->lock, &resp->lock));
@@ -138,7 +135,7 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
                dprintk("lockd: TEST          status %d vers %d\n",
                        ntohl(resp->status), rqstp->rq_vers);
 
Quoted 8 lines
Quoted 8 lines
fs/lockd/svcproc.c +1 -2
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;
--

2.51.0

--
Jeff Layton <[email protected]>

maintainer
#17 lore raw
Quoted 416 lines · Olga Kornievskaia
Quoted 415 lines · Jeff Layton
Quoted 412 lines · Olga Kornievskaia
Quoted 383 lines · Jeff Layton
Quoted 289 lines · Jeff Layton
Quoted 288 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 656

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +0 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
Quoted 10 lines
Quoted 5 lines
fs/lockd/clntproc.c +2 -0
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;

Sorry, I take it back. That alone won't fix it because of the pointer- saving shenanigans in __nlm4svc_proc_test. We'll need to do this a bit more carefully, I think.

How about this patch? The changelog still needs some work, but I think this is the most correct way to handle it:

-------------------------8<----------------------------

[PATCH] nlm: fix handling of conflocks by NLM

The handling of conflocks can result in a refcount overput on the flc_owner in the case of a reexported NFSv3 fs.

lockd will pass down a file_lock structure in its arguments with flc_owner set to an NLM owner and a valid reference. Once that gets down to the reexported NFS client, it will ignore that field and leave it intact when copying in conflock info.

The NLM server code will then end up releasing the conflock, and then releasing the one from the arguments, not realizing that the lock has already been released.

The owner info in the arguments to vfs_test_lock() is there to give information about the requestor. Once nlmclnt_test() is going to copy in the conflock info however, it needs to release the old owner.

Have nlmclnt_test() do call locks_release_private() after testing the lock to release any info that was in the old file_lock structure.

This creates another problem: __nlm4svc_proc_test() and __nlmsvc_proc_test() both call nlmsvc_put_lockowner() unconditionally on the original lockowner that it got from decoding the args. Switch them both to just call locks_release_private() on &argp->lock instead, which should handle the case properly where the info in argp->lock has already been released.

With that changed, we can also eliminate the call to locks_release_private() in nlmsvc_testlock() since the callers will now handle that.

---
 fs/lockd/clntproc.c | 3 +++
 fs/lockd/svc4proc.c | 4 +---
 fs/lockd/svclock.c  | 1 -
 fs/lockd/svcproc.c  | 5 +----
 4 files changed, 5 insertions(+), 8 deletions(-)
fs/lockd/clntproc.c +3 -0
index cebcc283b7ce..d3dd04137677 100644
@@ -438,6 +438,9 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
        if (status < 0)
                goto out;
 
+       /* Release any references held by fl before copying in conflock info */
+       locks_release_private(fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;
fs/lockd/svc4proc.c +0 -2
index 109e5caae8c7..cbfec12296a4 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
        /* Now check for conflicting locks */
        resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
                                       &resp->lock);

fs/lockd/svc4proc.c __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp) · line 120

Quoted 8 lines
fs/lockd/svc4proc.c +1 -1
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        else
                dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;

Aren't these svc4proc changes defeat the purpose of the following commit:

commit 184cefbe62627730c30282df12bcff9aae4816ea

Author: Benjamin Coddington <[email protected]>
Date:   Mon Jun 13 09:40:06 2022 -0400

NLM: Defend against file_lock changes after vfs_test_lock()

Instead of trusting that struct file_lock returns completely unchanged after vfs_test_lock() when there's no conflicting lock, stash away our nlm_lockowner reference so we can properly release it for all cases.

This defends against another file_lock implementation overwriting fl_owner when the return type is F_UNLCK.

Not that they are the cause of the crash but I would imagine it would cause a problem fixed by the patch....

Quoted 6 lines
Quoted 1 line

Yes, it would. I'm not sure that patch is correct.

But it seems wrong that we are now considering putting a patch that fixes one issue (ie crash on reexport) but re-introduces another issue (that the reverted code fixed be it not in a correct manner).

I'm not sure what you mean by reintroducing another issue? What bug would be reintroduced?

My main concern is that when you clear fl_lmops pointer in the way that you were suggesting, you're making assumptions about the lockowner that eventually ends up in flc_owner.

Calling vfs_test_lock() may populate flc_owner with a valid reference. Maybe it ends up returning an nfsd4 lock in that field. At that point you've just leaked that reference.

The right fix here is doing what Neil suggested -- adding a separate conflock argument to the ->lock operation. That would fix all of this. A lot of filesystems have ->lock operations though and they will all need to be touched.

How about we just do this:

Make lockd's client side respect EXPORT_OP_NOLOCKS just like the nfs4 client does. That would prevent this particular problem (albeit with a heavy hammer). That should be easy to backport to stable too.

Then we do a separate patch series that adds a separate conflock parameter to the ->lock f_op and vfs_test_lock()

Thoughts?

Signature · 1 line
Jeff Layton <[email protected]>
#18 lore raw
Quoted 420 lines · Jeff Layton
Quoted 418 lines · Olga Kornievskaia
Quoted 416 lines · Jeff Layton
Quoted 406 lines · Olga Kornievskaia
Quoted 383 lines · Jeff Layton
Quoted 289 lines · Jeff Layton
Quoted 288 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 656

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +0 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
Quoted 10 lines
Quoted 5 lines
fs/lockd/clntproc.c +2 -0
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;

Sorry, I take it back. That alone won't fix it because of the pointer- saving shenanigans in __nlm4svc_proc_test. We'll need to do this a bit more carefully, I think.

How about this patch? The changelog still needs some work, but I think this is the most correct way to handle it:

-------------------------8<----------------------------

[PATCH] nlm: fix handling of conflocks by NLM

The handling of conflocks can result in a refcount overput on the flc_owner in the case of a reexported NFSv3 fs.

lockd will pass down a file_lock structure in its arguments with flc_owner set to an NLM owner and a valid reference. Once that gets down to the reexported NFS client, it will ignore that field and leave it intact when copying in conflock info.

The NLM server code will then end up releasing the conflock, and then releasing the one from the arguments, not realizing that the lock has already been released.

The owner info in the arguments to vfs_test_lock() is there to give information about the requestor. Once nlmclnt_test() is going to copy in the conflock info however, it needs to release the old owner.

Have nlmclnt_test() do call locks_release_private() after testing the lock to release any info that was in the old file_lock structure.

This creates another problem: __nlm4svc_proc_test() and __nlmsvc_proc_test() both call nlmsvc_put_lockowner() unconditionally on the original lockowner that it got from decoding the args. Switch them both to just call locks_release_private() on &argp->lock instead, which should handle the case properly where the info in argp->lock has already been released.

With that changed, we can also eliminate the call to locks_release_private() in nlmsvc_testlock() since the callers will now handle that.

---
 fs/lockd/clntproc.c | 3 +++
 fs/lockd/svc4proc.c | 4 +---
 fs/lockd/svclock.c  | 1 -
 fs/lockd/svcproc.c  | 5 +----
 4 files changed, 5 insertions(+), 8 deletions(-)
fs/lockd/clntproc.c +3 -0
index cebcc283b7ce..d3dd04137677 100644
@@ -438,6 +438,9 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
        if (status < 0)
                goto out;
 
+       /* Release any references held by fl before copying in conflock info */
+       locks_release_private(fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;
fs/lockd/svc4proc.c +0 -2
index 109e5caae8c7..cbfec12296a4 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
        /* Now check for conflicting locks */
        resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
                                       &resp->lock);

fs/lockd/svc4proc.c __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp) · line 120

Quoted 8 lines
fs/lockd/svc4proc.c +1 -1
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        else
                dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;

Aren't these svc4proc changes defeat the purpose of the following commit:

commit 184cefbe62627730c30282df12bcff9aae4816ea

Author: Benjamin Coddington <[email protected]>
Date:   Mon Jun 13 09:40:06 2022 -0400

NLM: Defend against file_lock changes after vfs_test_lock()

Instead of trusting that struct file_lock returns completely unchanged after vfs_test_lock() when there's no conflicting lock, stash away our nlm_lockowner reference so we can properly release it for all cases.

Quoted 6 lines

This defends against another file_lock implementation overwriting fl_owner when the return type is F_UNLCK.

Not that they are the cause of the crash but I would imagine it would cause a problem fixed by the patch....

Yes, it would. I'm not sure that patch is correct.

Quoted 7 lines
Quoted 4 lines

But it seems wrong that we are now considering putting a patch that fixes one issue (ie crash on reexport) but re-introduces another issue (that the reverted code fixed be it not in a correct manner).

I'm not sure what you mean by reintroducing another issue? What bug would be reintroduced?

I might be reading the changes incorrectly so bear with me. The proposal that in nlm4srv_proc_test() we don't save the tets_owner pointer prior to calling nlmsvc_testlock() and then do a put on the original file (args->lock.fl). now this lock is the one being modified by vfs_test_lock() am I not correct?

When you asked what bug would be reintroduced. Here's what I got from a bugzilla opened with regards to the "NLM: Defend against file_lock changes after vfs_test_lock()" patch. I believe it describes the problem that would come back.

"If underlying filesystem implementation modifies file_lock->fl_owner for F_GETLK operations, returned owner may not be even a mapped address on the box. This scenario, when dealing with a non-conflictive locks may cause oops, panicing the box while accessing unmapped address trying to get rid of the owner through :

__nlm4svc_proc_test() / __nlm4svc_proc_test() -> nlmsvc_release_lockowner() -> nlmsvc_put_lockowner

oops stack :

[6823679.912979] RIP: 0010:nlmsvc_put_lockowner+0x6/0x80 [lockd]
....
[6823679.923427] Call Trace:
[6823679.924012]  __nlm4svc_proc_test+0xe8/0x110 [lockd]
[6823679.924674]  svc_process_common+0x64f/0x700 [sunrpc]
[6823679.925273]  ? svc_recv+0x3d0/0x820 [sunrpc]
[6823679.925823]  ? grace_ender+0x20/0x20 [lockd]
[6823679.926388]  svc_process+0xb7/0xf0 [sunrpc]
[6823679.926949]  lockd+0xae/0x190 [lockd]
[6823679.927467]  ? __kthread_parkme+0x4c/0x70
[6823679.927976]  kthread+0x116/0x130
[6823679.928467]  ? kthread_flush_work_fn+0x10/0x10
[6823679.928972]  ret_from_fork+0x1f/0x40

POSIX imposes flock structures to remain intact (but l_type set to F_UNLCK) for F_GETLK cases when lock is not held,
but not necessary file_lock->fl_owner struct. NLM should be more defensive on that situation if underlying fs implementation changes the owner.

Steps to reproduce:
1.GPFS cluster, several nodes acting as NFS server 2.adv. locking operations over nfs3 shares "

Quoted 7 lines

My main concern is that when you clear fl_lmops pointer in the way that you were suggesting, you're making assumptions about the lockowner that eventually ends up in flc_owner.

Calling vfs_test_lock() may populate flc_owner with a valid reference. Maybe it ends up returning an nfsd4 lock in that field. At that point you've just leaked that reference.

I'm rather unclear here why a lock that's being tested is supposed to be responsible for releasing something on a conflicting lock. I would rather think that like an example of GPFS it is not something we should be touching but why is a conflicting NFSD lock (saying coming from a different namespace) should be any different. On that basis I thought my solution is a plausible stepping stone

Quoted 17 lines

The right fix here is doing what Neil suggested -- adding a separate conflock argument to the ->lock operation. That would fix all of this. A lot of filesystems have ->lock operations though and they will all need to be touched.

How about we just do this:

Make lockd's client side respect EXPORT_OP_NOLOCKS just like the nfs4 client does. That would prevent this particular problem (albeit with a heavy hammer). That should be easy to backport to stable too.

Then we do a separate patch series that adds a separate conflock parameter to the ->lock f_op and vfs_test_lock()

Thoughts?
--
Jeff Layton <[email protected]>

maintainer
#19 lore raw
Quoted 484 lines · Olga Kornievskaia
Quoted 420 lines · Jeff Layton
Quoted 418 lines · Olga Kornievskaia
Quoted 416 lines · Jeff Layton
Quoted 406 lines · Olga Kornievskaia
Quoted 383 lines · Jeff Layton
Quoted 289 lines · Jeff Layton
Quoted 288 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in
calling nlmsvc_testlock() with a lock conflict lock_release_private()
call would end up calling nlmsvc_put_lockowner() and then back in
nlm4svc_proc_test() function there will be another call to
nlmsvc_put_lockowner() for the same owner leading to use-after-free
violation (below).

The problem only arises when the underlying file system has been
re-exported as different paths are taken in vfs_test_lock().
When it's reexport, filp->f_op->lock is set and when vfs_test_lock()
is done fl_lmops pointer is non-null. When it's regular export,
vfs_test_lock() calls posix_test_lock() which ends up calling
locks_copy_conflock() and it copies NULL into fl_lmops and then
calling into lock_release_private() does not call
nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to
make sure that if there is a conflict (be it a local file system
or reexport), lock_release_private() would not call
nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092
kernel:
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel: show_stack+0x34/0x98 (C)
kernel: dump_stack_lvl+0x80/0xa8
kernel: print_address_description.constprop.0+0x90/0x310
kernel: print_report+0x108/0x1f8
kernel: kasan_report+0xc8/0x120
kernel: kasan_check_range+0xe8/0x190
kernel: __kasan_check_read+0x20/0x30
kernel: nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel: nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel: nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel: svc_process_common+0xb20/0x17b0 [sunrpc]
kernel: svc_process+0x414/0x900 [sunrpc]
kernel: svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel: svc_recv+0x1a4/0x520 [sunrpc]
kernel: lockd+0x154/0x298 [lockd]
kernel: kthread+0x2f8/0x398
kernel: ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel: kasan_save_stack+0x3c/0x70
kernel: kasan_save_track+0x20/0x40
kernel: kasan_save_alloc_info+0x40/0x58
kernel: __kasan_kmalloc+0xd4/0xd8
kernel: __kmalloc_cache_noprof+0x1a8/0x5c0
kernel: nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel: nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel: __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel: nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel: nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel: svc_process_common+0xb20/0x17b0 [sunrpc]
kernel: svc_process+0x414/0x900 [sunrpc]
kernel: svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel: svc_recv+0x1a4/0x520 [sunrpc]
kernel: lockd+0x154/0x298 [lockd]
kernel: kthread+0x2f8/0x398
kernel: ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel: kasan_save_stack+0x3c/0x70
kernel: kasan_save_track+0x20/0x40
kernel: __kasan_save_free_info+0x4c/0x80
kernel: __kasan_slab_free+0x88/0xc0
kernel: kfree+0x110/0x480
kernel: nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel: nlmsvc_put_owner+0x18/0x30 [lockd]
kernel: locks_release_private+0x190/0x2a8
kernel: nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel: __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel: nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel: nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel: svc_process_common+0xb20/0x17b0 [sunrpc]
kernel: svc_process+0x414/0x900 [sunrpc]
kernel: svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel: svc_recv+0x1a4/0x520 [sunrpc]
kernel: lockd+0x154/0x298 [lockd]
kernel: kthread+0x2f8/0x398
kernel: ret_from_fork+0x10/0x20
kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of
freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)
kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:
kernel: Memory state around the buggy address:
kernel: ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: ^
kernel: ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G B 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel: show_stack+0x34/0x98 (C)
kernel: dump_stack_lvl+0x80/0xa8
kernel: dump_stack+0x1c/0x30
kernel: nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel: __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel: nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel: nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel: svc_process_common+0xb20/0x17b0 [sunrpc]
kernel: svc_process+0x414/0x900 [sunrpc]
kernel: svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel: svc_recv+0x1a4/0x520 [sunrpc]
kernel: lockd+0x154/0x298 [lockd]
kernel: kthread+0x2f8/0x398
kernel: ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G B 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel: refcount_dec_not_one+0x198/0x1b0 (P)
kernel: refcount_dec_and_lock+0x1c/0xb8
kernel: nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel: __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel: nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel: nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel: svc_process_common+0xb20/0x17b0 [sunrpc]
kernel: svc_process+0x414/0x900 [sunrpc]
kernel: svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel: svc_recv+0x1a4/0x520 [sunrpc]
kernel: lockd+0x154/0x298 [lockd]
kernel: kthread+0x2f8/0x398
kernel: ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---

Signed-off-by: Olga Kornievskaia <[email protected]>
---
fs/lockd/svclock.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/fs/lockd/svclock.c b/fs/lockd/svclock.c
index a31dc9588eb8..1dd0fec186de 100644
--- a/fs/lockd/svclock.c
+++ b/fs/lockd/svclock.c
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
conflock->fl.c.flc_type = lock->fl.c.flc_type;
conflock->fl.fl_start = lock->fl.fl_start;
conflock->fl.fl_end = lock->fl.fl_end;
+ conflock->fl.fl_lmops = NULL;
locks_release_private(&lock->fl);

Quoted 3 lines
Quoted 1 line

ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +0 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
Quoted 10 lines
Quoted 5 lines
fs/lockd/clntproc.c +2 -0
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;

Sorry, I take it back. That alone won't fix it because of the pointer- saving shenanigans in __nlm4svc_proc_test. We'll need to do this a bit more carefully, I think.

How about this patch? The changelog still needs some work, but I think this is the most correct way to handle it:

-------------------------8<----------------------------

[PATCH] nlm: fix handling of conflocks by NLM

The handling of conflocks can result in a refcount overput on the flc_owner in the case of a reexported NFSv3 fs.

lockd will pass down a file_lock structure in its arguments with flc_owner set to an NLM owner and a valid reference. Once that gets down to the reexported NFS client, it will ignore that field and leave it intact when copying in conflock info.

The NLM server code will then end up releasing the conflock, and then releasing the one from the arguments, not realizing that the lock has already been released.

The owner info in the arguments to vfs_test_lock() is there to give information about the requestor. Once nlmclnt_test() is going to copy in the conflock info however, it needs to release the old owner.

Have nlmclnt_test() do call locks_release_private() after testing the lock to release any info that was in the old file_lock structure.

This creates another problem: __nlm4svc_proc_test() and __nlmsvc_proc_test() both call nlmsvc_put_lockowner() unconditionally on the original lockowner that it got from decoding the args. Switch them both to just call locks_release_private() on &argp->lock instead, which should handle the case properly where the info in argp->lock has already been released.

With that changed, we can also eliminate the call to locks_release_private() in nlmsvc_testlock() since the callers will now handle that.

---
 fs/lockd/clntproc.c | 3 +++
 fs/lockd/svc4proc.c | 4 +---
 fs/lockd/svclock.c  | 1 -
 fs/lockd/svcproc.c  | 5 +----
 4 files changed, 5 insertions(+), 8 deletions(-)
fs/lockd/clntproc.c +3 -0
index cebcc283b7ce..d3dd04137677 100644
@@ -438,6 +438,9 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
        if (status < 0)
                goto out;
 
+       /* Release any references held by fl before copying in conflock info */
+       locks_release_private(fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;
fs/lockd/svc4proc.c +0 -2
index 109e5caae8c7..cbfec12296a4 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
        /* Now check for conflicting locks */
        resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
                                       &resp->lock);

fs/lockd/svc4proc.c __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp) · line 120

Quoted 8 lines
fs/lockd/svc4proc.c +1 -1
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        else
                dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-       nlmsvc_put_lockowner(test_owner);
+       locks_release_private(&argp->lock.fl);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;

Aren't these svc4proc changes defeat the purpose of the following commit:

commit 184cefbe62627730c30282df12bcff9aae4816ea

Author: Benjamin Coddington <[email protected]>
Date:   Mon Jun 13 09:40:06 2022 -0400

NLM: Defend against file_lock changes after vfs_test_lock()

Instead of trusting that struct file_lock returns completely unchanged after vfs_test_lock() when there's no conflicting lock, stash away our nlm_lockowner reference so we can properly release it for all cases.

Quoted 6 lines

This defends against another file_lock implementation overwriting fl_owner when the return type is F_UNLCK.

Not that they are the cause of the crash but I would imagine it would cause a problem fixed by the patch....

Yes, it would. I'm not sure that patch is correct.

Quoted 7 lines
Quoted 4 lines

But it seems wrong that we are now considering putting a patch that fixes one issue (ie crash on reexport) but re-introduces another issue (that the reverted code fixed be it not in a correct manner).

I'm not sure what you mean by reintroducing another issue? What bug would be reintroduced?

I might be reading the changes incorrectly so bear with me. The proposal that in nlm4srv_proc_test() we don't save the tets_owner pointer prior to calling nlmsvc_testlock() and then do a put on the original file (args->lock.fl). now this lock is the one being modified by vfs_test_lock() am I not correct?

When you asked what bug would be reintroduced. Here's what I got from a bugzilla opened with regards to the "NLM: Defend against file_lock changes after vfs_test_lock()" patch. I believe it describes the problem that would come back.

"If underlying filesystem implementation modifies file_lock->fl_owner for F_GETLK operations, returned owner may not be even a mapped address on the box. This scenario, when dealing with a non-conflictive locks may cause oops, panicing the box while accessing unmapped address trying to get rid of the owner through :

__nlm4svc_proc_test() / __nlm4svc_proc_test() -> nlmsvc_release_lockowner() -> nlmsvc_put_lockowner

oops stack :

[6823679.912979] RIP: 0010:nlmsvc_put_lockowner+0x6/0x80 [lockd]
....
[6823679.923427] Call Trace:
[6823679.924012]  __nlm4svc_proc_test+0xe8/0x110 [lockd]
[6823679.924674]  svc_process_common+0x64f/0x700 [sunrpc]
[6823679.925273]  ? svc_recv+0x3d0/0x820 [sunrpc]
[6823679.925823]  ? grace_ender+0x20/0x20 [lockd]
[6823679.926388]  svc_process+0xb7/0xf0 [sunrpc]
[6823679.926949]  lockd+0xae/0x190 [lockd]
[6823679.927467]  ? __kthread_parkme+0x4c/0x70
[6823679.927976]  kthread+0x116/0x130
[6823679.928467]  ? kthread_flush_work_fn+0x10/0x10
[6823679.928972]  ret_from_fork+0x1f/0x40

POSIX imposes flock structures to remain intact (but l_type set to F_UNLCK) for F_GETLK cases when lock is not held,
but not necessary file_lock->fl_owner struct. NLM should be more defensive on that situation if underlying fs implementation changes the owner.

Steps to reproduce:
1.GPFS cluster, several nodes acting as NFS server 2.adv. locking operations over nfs3 shares "

Quoted 7 lines

My main concern is that when you clear fl_lmops pointer in the way that you were suggesting, you're making assumptions about the lockowner that eventually ends up in flc_owner.

Calling vfs_test_lock() may populate flc_owner with a valid reference. Maybe it ends up returning an nfsd4 lock in that field. At that point you've just leaked that reference.

Quoted 7 lines

I'm rather unclear here why a lock that's being tested is supposed to be responsible for releasing something on a conflicting lock. I would rather think that like an example of GPFS it is not something we should be touching but why is a conflicting NFSD lock (saying coming from a different namespace) should be any different. On that basis I thought my solution is a plausible stepping stone

I think I'm coming around to your way of thinking. The way I rationalize it is that the exported fs has to treat the flc_owner as opaque. Given that, we don't need to mess with the refcounting in the conflock.

I think Neil's version of this patch makes this a little clearer than your one-liner, but they're both basically the same approach. Either way, we'll need some good comments around this as leaving fl_lmops set to NULL in order to achieve this is quite subtle.

Signature · 1 line
Jeff Layton <[email protected]>
#20 lore raw
In reply to Jeff Layton
Quoted 5 lines · Jeff Layton

Longer term, I think Neil is right and we probably need to fix vfs_test_lock and the lock inode_operation to take a separate conflock for testlock purposes. That's a bigger change though (particularly the ->lock operations).

Thanks. But in the shorter term I'd like to suggest this.
I haven't tested, but I think it should fix the lockd issue and make the code cleaner too.
Sorry - I haven't written a commit description yet :-(
As nlmsvc_testlock() is being passed a conflock, use that directly to hold the found lock, and initialise it from 'lock' before, rather than copying results the from lock after.

NeilBrown

fs/lockd/svc4proc.c +1 -3
index 109e5caae8c7..4b6f18d97734 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	struct nlm_args *argp = rqstp->rq_argp;
 	struct nlm_host	*host;
 	struct nlm_file	*file;
-	struct nlm_lockowner *test_owner;
 	__be32 rc = rpc_success;
 
 	dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-	test_owner = argp->lock.fl.c.flc_owner;
 	/* Now check for conflicting locks */
 	resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
 				       &resp->lock);
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	else
 		dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-	nlmsvc_put_lockowner(test_owner);
+	nlmsvc_release_lockowner(&argp->lock);
 	nlmsvc_release_host(host);
 	nlm_release_file(file);
 	return rc;
fs/lockd/svclock.c +9 -6
index a31dc9588eb8..381b837a8c18 100644
@@ -627,7 +627,13 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
 	}
 
 	mode = lock_to_openmode(&lock->fl);
-	error = vfs_test_lock(file->f_file[mode], &lock->fl);
+	locks_init_lock(&conflock->fl);
+	/* vfs_test_lock only uses start, end, and owner, but tests flc_file */
+	conflock->fl.c.flc_file = lock->fl.c.flc_file;
+	conflock->fl.fl_start = lock->fl.fl_start;
+	conflock->fl.fl_end = lock->fl.fl_end;
+	conflock->fl.c.flc_owner = lock->fl.c.flc_owner;
+	error = vfs_test_lock(file->f_file[mode], &conflock->fl);
 	if (error) {
 		/* We can't currently deal with deferred test requests */
 		if (error == FILE_LOCK_DEFERRED)
@@ -648,11 +654,8 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
 	conflock->caller = "somehost";	/* FIXME */
 	conflock->len = strlen(conflock->caller);
 	conflock->oh.len = 0;		/* don't return OH info */
-	conflock->svid = lock->fl.c.flc_pid;
-	conflock->fl.c.flc_type = lock->fl.c.flc_type;
-	conflock->fl.fl_start = lock->fl.fl_start;
-	conflock->fl.fl_end = lock->fl.fl_end;
-	locks_release_private(&lock->fl);
+	conflock->svid = conflock->fl.c.flc_pid;
+	locks_release_private(&conflock->fl);
 
 	ret = nlm_lck_denied;
 out:
fs/lockd/svcproc.c +1 -4
index f53d5177f267..5817ef272332 100644
@@ -117,7 +117,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	struct nlm_args *argp = rqstp->rq_argp;
 	struct nlm_host	*host;
 	struct nlm_file	*file;
-	struct nlm_lockowner *test_owner;
 	__be32 rc = rpc_success;
 
 	dprintk("lockd: TEST          called\n");
@@ -127,8 +126,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-	test_owner = argp->lock.fl.c.flc_owner;
-
 	/* Now check for conflicting locks */
 	resp->status = cast_status(nlmsvc_testlock(rqstp, file, host,
 						   &argp->lock, &resp->lock));
@@ -138,7 +135,7 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 		dprintk("lockd: TEST          status %d vers %d\n",
 			ntohl(resp->status), rqstp->rq_vers);
 
-	nlmsvc_put_lockowner(test_owner);
+	nlmsvc_release_lockowner(&argp->lock);
 	nlmsvc_release_host(host);
 	nlm_release_file(file);
 	return rc;
#21 lore raw
Quoted 8 lines · NeilBrown
Quoted 5 lines · Jeff Layton

Longer term, I think Neil is right and we probably need to fix vfs_test_lock and the lock inode_operation to take a separate conflock for testlock purposes. That's a bigger change though (particularly the ->lock operations).

Thanks. But in the shorter term I'd like to suggest this.

Then again .. I'm less convinced about the separate conflock for vfs_test_lock() and friends. However adding this to my previous patch would be good I think.

fs/locks.c +3 -1
index 04a3f0e20724..200a31e3c4b8 100644
@@ -2188,10 +2188,12 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
  * @fl: The lock to test; also used to hold result
  *
  * Returns -ERRNO on failure.  Indicates presence of conflicting lock by
- * setting conf->fl_type to something other than F_UNLCK.
+ * setting fl->fl_type to something other than F_UNLCK.
+ * Both fl_lmops and fl_ops in @fl must be NULL.
  */
 int vfs_test_lock(struct file *filp, struct file_lock *fl)
 {
+	WARN_ON_ONCE(fl->fl_ops || fl->fl_lmops);
 	WARN_ON_ONCE(filp != fl->c.flc_file);
 	if (filp->f_op->lock)
 		return filp->f_op->lock(filp, F_GETLK, fl);

Also I think it would clean code a lot if we splice the "lock" file_operation into set_lock, get_lock, and remove_lock (or similar). It would make grepping easier too.

NeilBrown

maintainer
#22 lore raw
In reply to NeilBrown
Quoted 51 lines · NeilBrown
Quoted 5 lines · Jeff Layton

Longer term, I think Neil is right and we probably need to fix vfs_test_lock and the lock inode_operation to take a separate conflock for testlock purposes. That's a bigger change though (particularly the ->lock operations).

Thanks. But in the shorter term I'd like to suggest this.
I haven't tested, but I think it should fix the lockd issue and make the code cleaner too.
Sorry - I haven't written a commit description yet :-(
As nlmsvc_testlock() is being passed a conflock, use that directly to hold the found lock, and initialise it from 'lock' before, rather than copying results the from lock after.

NeilBrown

fs/lockd/svc4proc.c +1 -3
index 109e5caae8c7..4b6f18d97734 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	struct nlm_args *argp = rqstp->rq_argp;
 	struct nlm_host	*host;
 	struct nlm_file	*file;
-	struct nlm_lockowner *test_owner;
 	__be32 rc = rpc_success;
 
 	dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-	test_owner = argp->lock.fl.c.flc_owner;
 	/* Now check for conflicting locks */
 	resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
 				       &resp->lock);
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	else
 		dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-	nlmsvc_put_lockowner(test_owner);
+	nlmsvc_release_lockowner(&argp->lock);
 	nlmsvc_release_host(host);
 	nlm_release_file(file);
 	return rc;
fs/lockd/svclock.c +0 -0
index a31dc9588eb8..381b837a8c18 100644

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 635

Quoted 10 lines
fs/lockd/svclock.c +6 -1
@@ -627,7 +627,13 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
 	}
 
 	mode = lock_to_openmode(&lock->fl);
-	error = vfs_test_lock(file->f_file[mode], &lock->fl);
+	locks_init_lock(&conflock->fl);
+	/* vfs_test_lock only uses start, end, and owner, but tests flc_file */
+	conflock->fl.c.flc_file = lock->fl.c.flc_file;
+	conflock->fl.fl_start = lock->fl.fl_start;
+	conflock->fl.fl_end = lock->fl.fl_end;
+	conflock->fl.c.flc_owner = lock->fl.c.flc_owner;

This is basically the same approach that Olga took. This leaves the fl_lmops field unset, and so we will just not do the refcounting in this case.

I'm starting to be convinced of that approach -- the exported filesystem's ->lock operation should just treat flc_owner as opaque. We'll need a comment around this though, since setting fl_lmops to NULL for this reason isn't intuitive.

Quoted 48 lines
fs/lockd/svclock.c +3 -5
+	error = vfs_test_lock(file->f_file[mode], &conflock->fl);
 	if (error) {
 		/* We can't currently deal with deferred test requests */
 		if (error == FILE_LOCK_DEFERRED)
@@ -648,11 +654,8 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
 	conflock->caller = "somehost";	/* FIXME */
 	conflock->len = strlen(conflock->caller);
 	conflock->oh.len = 0;		/* don't return OH info */
-	conflock->svid = lock->fl.c.flc_pid;
-	conflock->fl.c.flc_type = lock->fl.c.flc_type;
-	conflock->fl.fl_start = lock->fl.fl_start;
-	conflock->fl.fl_end = lock->fl.fl_end;
-	locks_release_private(&lock->fl);
+	conflock->svid = conflock->fl.c.flc_pid;
+	locks_release_private(&conflock->fl);
 
 	ret = nlm_lck_denied;
 out:
fs/lockd/svcproc.c +1 -4
index f53d5177f267..5817ef272332 100644
@@ -117,7 +117,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	struct nlm_args *argp = rqstp->rq_argp;
 	struct nlm_host	*host;
 	struct nlm_file	*file;
-	struct nlm_lockowner *test_owner;
 	__be32 rc = rpc_success;
 
 	dprintk("lockd: TEST          called\n");
@@ -127,8 +126,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-	test_owner = argp->lock.fl.c.flc_owner;
-
 	/* Now check for conflicting locks */
 	resp->status = cast_status(nlmsvc_testlock(rqstp, file, host,
 						   &argp->lock, &resp->lock));
@@ -138,7 +135,7 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
 		dprintk("lockd: TEST          status %d vers %d\n",
 			ntohl(resp->status), rqstp->rq_vers);
 
-	nlmsvc_put_lockowner(test_owner);
+	nlmsvc_release_lockowner(&argp->lock);
 	nlmsvc_release_host(host);
 	nlm_release_file(file);
 	return rc;
Signature · 1 line
Jeff Layton <[email protected]>
#23 lore raw
In reply to NeilBrown
Quoted 103 lines · NeilBrown
Quoted 5 lines · Jeff Layton

Longer term, I think Neil is right and we probably need to fix vfs_test_lock and the lock inode_operation to take a separate conflock for testlock purposes. That's a bigger change though (particularly the ->lock operations).

Thanks. But in the shorter term I'd like to suggest this.
I haven't tested, but I think it should fix the lockd issue and make the code cleaner too.
Sorry - I haven't written a commit description yet :-(
As nlmsvc_testlock() is being passed a conflock, use that directly to hold the found lock, and initialise it from 'lock' before, rather than copying results the from lock after.

NeilBrown

fs/lockd/svc4proc.c +1 -3
index 109e5caae8c7..4b6f18d97734 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
        /* Now check for conflicting locks */
        resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
                                       &resp->lock);
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        else
                dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-       nlmsvc_put_lockowner(test_owner);
+       nlmsvc_release_lockowner(&argp->lock);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;
fs/lockd/svclock.c +9 -6
index a31dc9588eb8..381b837a8c18 100644
@@ -627,7 +627,13 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
        }
 
        mode = lock_to_openmode(&lock->fl);
-       error = vfs_test_lock(file->f_file[mode], &lock->fl);
+       locks_init_lock(&conflock->fl);
+       /* vfs_test_lock only uses start, end, and owner, but tests flc_file */
+       conflock->fl.c.flc_file = lock->fl.c.flc_file;
+       conflock->fl.fl_start = lock->fl.fl_start;
+       conflock->fl.fl_end = lock->fl.fl_end;
+       conflock->fl.c.flc_owner = lock->fl.c.flc_owner;
+       error = vfs_test_lock(file->f_file[mode], &conflock->fl);
        if (error) {
                /* We can't currently deal with deferred test requests */
                if (error == FILE_LOCK_DEFERRED)
@@ -648,11 +654,8 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
        conflock->caller = "somehost";  /* FIXME */
        conflock->len = strlen(conflock->caller);
        conflock->oh.len = 0;           /* don't return OH info */
-       conflock->svid = lock->fl.c.flc_pid;
-       conflock->fl.c.flc_type = lock->fl.c.flc_type;
-       conflock->fl.fl_start = lock->fl.fl_start;
-       conflock->fl.fl_end = lock->fl.fl_end;
-       locks_release_private(&lock->fl);
+       conflock->svid = conflock->fl.c.flc_pid;
+       locks_release_private(&conflock->fl);
 
        ret = nlm_lck_denied;
 out:
fs/lockd/svcproc.c +0 -3
index f53d5177f267..5817ef272332 100644
@@ -117,7 +117,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST          called\n");
@@ -127,8 +126,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
-
        /* Now check for conflicting locks */
        resp->status = cast_status(nlmsvc_testlock(rqstp, file, host,
                                                   &argp->lock, &resp->lock));

fs/lockd/svcproc.c __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp) · line 141

Quoted 8 lines
fs/lockd/svcproc.c +1 -1
@@ -138,7 +135,7 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
                dprintk("lockd: TEST          status %d vers %d\n",
                        ntohl(resp->status), rqstp->rq_vers);
 
-       nlmsvc_put_lockowner(test_owner);
+       nlmsvc_release_lockowner(&argp->lock);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;

No crash with this patch.

#24 lore raw
Quoted 107 lines · Olga Kornievskaia
Quoted 105 lines · NeilBrown
Quoted 5 lines · Jeff Layton

Longer term, I think Neil is right and we probably need to fix vfs_test_lock and the lock inode_operation to take a separate conflock for testlock purposes. That's a bigger change though (particularly the ->lock operations).

Thanks. But in the shorter term I'd like to suggest this.
I haven't tested, but I think it should fix the lockd issue and make the code cleaner too.
Sorry - I haven't written a commit description yet :-(
As nlmsvc_testlock() is being passed a conflock, use that directly to hold the found lock, and initialise it from 'lock' before, rather than copying results the from lock after.

NeilBrown

fs/lockd/svc4proc.c +1 -3
index 109e5caae8c7..4b6f18d97734 100644
@@ -97,7 +97,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST4        called\n");
@@ -107,7 +106,6 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
        /* Now check for conflicting locks */
        resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock,
                                       &resp->lock);
@@ -116,7 +114,7 @@ __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        else
                dprintk("lockd: TEST4        status %d\n", ntohl(resp->status));
 
-       nlmsvc_put_lockowner(test_owner);
+       nlmsvc_release_lockowner(&argp->lock);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;
fs/lockd/svclock.c +9 -6
index a31dc9588eb8..381b837a8c18 100644
@@ -627,7 +627,13 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
        }
 
        mode = lock_to_openmode(&lock->fl);
-       error = vfs_test_lock(file->f_file[mode], &lock->fl);
+       locks_init_lock(&conflock->fl);
+       /* vfs_test_lock only uses start, end, and owner, but tests flc_file */
+       conflock->fl.c.flc_file = lock->fl.c.flc_file;
+       conflock->fl.fl_start = lock->fl.fl_start;
+       conflock->fl.fl_end = lock->fl.fl_end;
+       conflock->fl.c.flc_owner = lock->fl.c.flc_owner;
+       error = vfs_test_lock(file->f_file[mode], &conflock->fl);
        if (error) {
                /* We can't currently deal with deferred test requests */
                if (error == FILE_LOCK_DEFERRED)
@@ -648,11 +654,8 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
        conflock->caller = "somehost";  /* FIXME */
        conflock->len = strlen(conflock->caller);
        conflock->oh.len = 0;           /* don't return OH info */
-       conflock->svid = lock->fl.c.flc_pid;
-       conflock->fl.c.flc_type = lock->fl.c.flc_type;
-       conflock->fl.fl_start = lock->fl.fl_start;
-       conflock->fl.fl_end = lock->fl.fl_end;
-       locks_release_private(&lock->fl);
+       conflock->svid = conflock->fl.c.flc_pid;
+       locks_release_private(&conflock->fl);
 
        ret = nlm_lck_denied;
 out:
fs/lockd/svcproc.c +0 -3
index f53d5177f267..5817ef272332 100644
@@ -117,7 +117,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        struct nlm_args *argp = rqstp->rq_argp;
        struct nlm_host *host;
        struct nlm_file *file;
-       struct nlm_lockowner *test_owner;
        __be32 rc = rpc_success;
 
        dprintk("lockd: TEST          called\n");
@@ -127,8 +126,6 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
        if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
                return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
 
-       test_owner = argp->lock.fl.c.flc_owner;
-
        /* Now check for conflicting locks */
        resp->status = cast_status(nlmsvc_testlock(rqstp, file, host,
                                                   &argp->lock, &resp->lock));
@@ -138,7 +135,7 @@ __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
                dprintk("lockd: TEST          status %d vers %d\n",
                        ntohl(resp->status), rqstp->rq_vers);
 
Quoted 8 lines

fs/lockd/svcproc.c __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp) · line 141

Quoted 6 lines
fs/lockd/svcproc.c +1 -1
-       nlmsvc_put_lockowner(test_owner);
+       nlmsvc_release_lockowner(&argp->lock);
        nlmsvc_release_host(host);
        nlm_release_file(file);
        return rc;

No crash with this patch.

Was the official fix for this problem posted and I missed it? If not, how can we make progress on the issue.

#25 lore raw
In reply to Jeff Layton
Quoted 265 lines · Jeff Layton
Quoted 243 lines · Olga Kornievskaia
Quoted 229 lines · Jeff Layton
Quoted 215 lines · Olga Kornievskaia
Quoted 183 lines · Jeff Layton
Quoted 181 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +1 -0
index a31dc9588eb8..1dd0fec186de 100644
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
      conflock->fl.c.flc_type = lock->fl.c.flc_type;
      conflock->fl.fl_start = lock->fl.fl_start;
      conflock->fl.fl_end = lock->fl.fl_end;
+     conflock->fl.fl_lmops = NULL;
      locks_release_private(&lock->fl);
 
Quoted 3 lines

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 658

Quoted 1 line
fs/lockd/svclock.c +0 -0
      ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

I have no claim that this solution is the best. I was contemplating on setting this to NULL only in the case when ->f_ops->lock() is NULL thus restricting it to the path that does not call posix_test_lock().

Quoted 3 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

IF lock_copy_conflock() is called then fl_lmops is already NULL.

Let me try to lay out the sequence of steps for both cases.

Reexport
1. when nlmsvc_test_lock() is called file->f_file[mode]->f_ops->lock is set (fl_lmops is set too) prior to calling vfs_test_lock.
2. Because ->lock is set vfs_test_lock() calls the ->lock function (instead of posix_test_lock)
3. After vfs_test_lock() fl_lmops is still set so lock_release_private is called and calls nlmscv_put_lockowner().

Normal export
1. when nlmsvc_test_lock() is called ->lock is not set (fl_lmops is set) prior to calling vfs_test_lock
2. Because -> is not set posix_test_lock() is called which will call local_copy_conflock() which will set fl_lmops to NULL.
3. Since fl_lmops is NULL put_lockowner isn't called.

Quoted 11 lines

Reexport is where I'm hazy. I'm assuming that reexported server opened a file and the "file" is the NFS file object and that's why file->f_file[mode]->f_ops->lock is set? So perhaps if we take the presence of ->lock to mean reexport, we can do as I did (ie., set fl_lmops to null), or maybe we can take an extra reference knowing that we'd need to put it in lock_release_private() ( -- this suggestion ties to your next question). I don't see any difference to either setting it to NULL or taking an extra reference for when ->lock() is set . Both are confusing and I would say warrant a comment for why we are doing it.

Quoted 7 lines

To be clear, there is nothing "special" about NFS reexport here. The NFS client just has some limitations as to what it can do when it's being (re)exported. Exporting other sorts of network or clustered filesystems can also be problematic for similar reasons.

So, we should focus on making this generically work, and not take -

Quoted 1 line

lock being set to mean that this is an NFS reexport.

I guess it doesn't matter what kind of re-export but the fact that if ->lock is set we know that posix_test_lock and then local_copy_conflock won't be called and thus there is a great chance that fl_lmops will still be set on return from vfs_test_lock().

Quoted 6 lines
Quoted 2 lines

A question: you mentioned this is a reexporting server. Are you reexporting an NFSv4 mount as NFSv3? Or is this a v3->v3 reexport?

v3->v3 reexport.

Ok, looking now:

The test_owner in __nlm4svc_proc_test() comes from the args, which happens during the decode phase. That gets a valid reference to an NLM lockowner in nlmsvc_locks_init_private(). Therefore, this call in __nlm4svc_proc_test() seems legit.

        nlmsvc_put_lockowner(test_owner);

Now, between those two gets/puts, there is the call to vfs_test_lock() in nlmsvc_testlock(). That uses the same file_lock argument structure to represent both the lock and the conflock.

Quoted 8 lines
        error = vfs_test_lock(file->f_file[mode], &lock->fl);

...so the flc_owner field at this point is still set to the (original) nlmclient's owner.

In this case, that call ends up calling down in nlmclnt_test(), which ignores the flc_owner field. It also ends up overwriting the other fields in &fl with conflock info.

I'm not seeing what you are seeing...

But I've been thinking what if the reexported file system was not NFS but some other file system that has f_ops->lock set. We would have lock_release_private() would find fl_lmops set and would lead to an (extra) free. So even if you think NLM's client side locking function is at fault here, I think the fix needs to be server-side nlm?

Quoted 24 lines

I think the bug is actually there. Before nlmclnt_test() overwrites the fields in "fl", it needs to release the owner reference (and zero out the owner). Would this patch fix the bug?

---------------------8------------------------

fs/lockd/clntproc.c +2 -0
index cebcc283b7ce..200309ee1a74 100644
@@ -438,6 +438,8 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock
 *fl)
        if (status < 0)
                goto out;
 
+       locks_release_private(&fl);
+
        switch (req->a_res.status) {
                case nlm_granted:
                        fl->c.flc_type = F_UNLCK;

--
Jeff Layton <[email protected]>

#26 lore raw
In reply to Jeff Layton
Quoted 186 lines · Jeff Layton
Quoted 175 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +0 -0
index a31dc9588eb8..1dd0fec186de 100644

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 658

Quoted 7 lines
fs/lockd/svclock.c +1 -0
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
 	conflock->fl.c.flc_type = lock->fl.c.flc_type;
 	conflock->fl.fl_start = lock->fl.fl_start;
 	conflock->fl.fl_end = lock->fl.fl_end;
+	conflock->fl.fl_lmops = NULL;
 	locks_release_private(&lock->fl);
 
 	ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

Quoted 6 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

Doesn't that mean that the real bug is that we're missing taking an owner reference in some case?

I had a bit of a look and .... and first it looked really strange that vfs_test_lock() uses the "fl" arg as both an "in" and an "out" - that is bound to cause refcounting issues.

But then I thought ... "no, wait" As an "in" parameter, "fl" is just a byte-range. fl_start and fl_end are all that matter. locks_overlap() uses these and nothing else of significance matters. In particular fl_owner doesn't and can't matter.

So why, e.g., does nfsd4_lockt() set file_lock->c.flc_owner at all?

And nlm4svc_retrieve_args() shouldn't be setting up "the missing parts of the file_lock structure" for TEST_LOCK - should it?

I think we need to treat get-lock quite differently from set-lock. After all vfs_lock_file() does have a separate "conf" arg, while vfs_test_lock() doesn't. That is an important difference.

Maybe we should change vfs_test_lock() to be passed
filp, start, len, *conf

and not pretend that we are passing in a lock description??

Thanks,
NeilBrown

maintainer
#27 lore raw
Quoted 194 lines · NeilBrown
Quoted 186 lines · Jeff Layton
Quoted 175 lines · Olga Kornievskaia

When knfsd is a reexport nfs server, it nlm4svc_proc_test() in calling nlmsvc_testlock() with a lock conflict lock_release_private() call would end up calling nlmsvc_put_lockowner() and then back in nlm4svc_proc_test() function there will be another call to nlmsvc_put_lockowner() for the same owner leading to use-after-free violation (below).

The problem only arises when the underlying file system has been re-exported as different paths are taken in vfs_test_lock(). When it's reexport, filp->f_op->lock is set and when vfs_test_lock() is done fl_lmops pointer is non-null. When it's regular export, vfs_test_lock() calls posix_test_lock() which ends up calling locks_copy_conflock() and it copies NULL into fl_lmops and then calling into lock_release_private() does not call nlmsvc_put_lockowner().

The proposed solution is to intentionally clear fl_lmops pointer to make sure that if there is a conflict (be it a local file system or reexport), lock_release_private() would not call nlmsvc_put_lockowner().

kernel: ==================================================================
kernel: BUG: KASAN: slab-use-after-free in nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel: Read of size 4 at addr ffff0000bf3bca10 by task lockd/6092 kernel:

kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Not tainted 6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  print_address_description.constprop.0+0x90/0x310
kernel:  print_report+0x108/0x1f8
kernel:  kasan_report+0xc8/0x120
kernel:  kasan_check_range+0xe8/0x190
kernel:  __kasan_check_read+0x20/0x30
kernel:  nlmsvc_put_lockowner+0x30/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Allocated by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  kasan_save_alloc_info+0x40/0x58
kernel:  __kasan_kmalloc+0xd4/0xd8
kernel:  __kmalloc_cache_noprof+0x1a8/0x5c0
kernel:  nlmsvc_locks_init_private+0xe4/0x520 [lockd]
kernel:  nlm4svc_retrieve_args+0x38c/0x530 [lockd]
kernel:  __nlm4svc_proc_test+0x194/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel:
kernel: Freed by task 6092:
kernel:  kasan_save_stack+0x3c/0x70
kernel:  kasan_save_track+0x20/0x40
kernel:  __kasan_save_free_info+0x4c/0x80
kernel:  __kasan_slab_free+0x88/0xc0
kernel:  kfree+0x110/0x480
kernel:  nlmsvc_put_lockowner+0x1b4/0x250 [lockd]
kernel:  nlmsvc_put_owner+0x18/0x30 [lockd]
kernel:  locks_release_private+0x190/0x2a8
kernel:  nlmsvc_testlock+0x2e0/0x648 [lockd]
kernel:  __nlm4svc_proc_test+0x244/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20

kernel:
kernel: The buggy address belongs to the object at ffff0000bf3bca00
which belongs to the cache kmalloc-64 of size 64
kernel: The buggy address is located 16 bytes inside of

        freed 64-byte region [ffff0000bf3bca00, ffff0000bf3bca40)

kernel:
kernel: The buggy address belongs to the physical page:
kernel: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x13f3bc
kernel: flags: 0x2fffff00000000(node=0|zone=2|lastcpupid=0xfffff)
kernel: page_type: f5(slab)
kernel: raw: 002fffff00000000 ffff0000800028c0 dead000000000122 0000000000000000
kernel: raw: 0000000000000000 0000000080200020 00000000f5000000 0000000000000000
kernel: page dumped because: kasan: bad access detected
kernel:

kernel: Memory state around the buggy address:
kernel:  ffff0000bf3bc900: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bc980: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel: >ffff0000bf3bca00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:                          ^
kernel:  ffff0000bf3bca80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
kernel:  ffff0000bf3bcb00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
kernel: ==================================================================
kernel: Disabling lock debugging due to kernel taint
kernel: AGLO: nlmsvc_put_lockowner 00000000028055fb count=0
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: Call trace:
kernel:  show_stack+0x34/0x98 (C)
kernel:  dump_stack_lvl+0x80/0xa8
kernel:  dump_stack+0x1c/0x30
kernel:  nlmsvc_put_lockowner+0x7c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ------------[ cut here ]------------
kernel: refcount_t: underflow; use-after-free.
kernel: WARNING: CPU: 0 PID: 6092 at lib/refcount.c:87 refcount_dec_not_one+0x198/0x1b0
kernel: Modules linked in: rpcrdma rdma_cm iw_cm ib_cm ib_core nfsd nfsv3 nfs_acl nfs lockd grace nfs_localio netfs ext4 crc16 mbcache jbd2 overlay uinput snd_seq_dummy snd_hrtimer qrtr rfkill vfat fat uvcvideo snd_hda_codec_generic videobuf2_vmalloc videobuf2_memops uvc snd_hda_intel videobuf2_v4l2 videobuf2_common snd_intel_dspcfg videodev snd_hda_codec snd_hda_core mc snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore sg loop auth_rpcgss vsock_loopback vmw_vsock_virtio_transport_common vmw_vsock_vmci_transport vmw_vmci vsock xfs 8021q garp stp llc mrp nvme nvme_core nvme_keyring nvme_auth ghash_ce hkdf e1000e sr_mod cdrom vmwgfx drm_ttm_helper ttm sunrpc dm_mirror dm_region_hash dm_log iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi fuse dm_multipath dm_mod nfnetlink
kernel: CPU: 0 UID: 0 PID: 6092 Comm: lockd Kdump: loaded Tainted: G    B               6.18.0-rc1+ #23 PREEMPT(voluntary)
kernel: Tainted: [B]=BAD_PAGE
kernel: Hardware name: VMware, Inc. VMware20,1/VBSA, BIOS VMW201.00V.24006586.BA64.2406042154 06/04/2024
kernel: pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
kernel: pc : refcount_dec_not_one+0x198/0x1b0
kernel: lr : refcount_dec_not_one+0x198/0x1b0
kernel: sp : ffff80008a627930
kernel: x29: ffff80008a627990 x28: ffff0000bf3bca00 x27: ffff0000ba5c7000
kernel: x26: 1fffe000191eeb84 x25: 1ffff000114c4f48 x24: ffff0000c8f75c24
kernel: x23: 0000000000000007 x22: ffff80008a627950 x21: 1ffff000114c4f26
kernel: x20: 00000000ffffffff x19: ffff0000bf3bca10 x18: 0000000000000310
kernel: x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
kernel: x14: 0000000000000000 x13: 0000000000000001 x12: ffff60004fd90aa3
kernel: x11: 1fffe0004fd90aa2 x10: ffff60004fd90aa2 x9 : dfff800000000000
kernel: x8 : 00009fffb026f55e x7 : ffff00027ec85513 x6 : 0000000000000001
kernel: x5 : ffff00027ec85510 x4 : ffff60004fd90aa3 x3 : ffff800080787bc0
kernel: x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000a75a8000
kernel: Call trace:
kernel:  refcount_dec_not_one+0x198/0x1b0 (P)
kernel:  refcount_dec_and_lock+0x1c/0xb8
kernel:  nlmsvc_put_lockowner+0x9c/0x250 [lockd]
kernel:  __nlm4svc_proc_test+0x2a8/0x318 [lockd]
kernel:  nlm4svc_proc_test+0x44/0xb78 [lockd]
kernel:  nlmsvc_dispatch+0xb0/0x200 [lockd]
kernel:  svc_process_common+0xb20/0x17b0 [sunrpc]
kernel:  svc_process+0x414/0x900 [sunrpc]
kernel:  svc_handle_xprt+0x5c8/0xd60 [sunrpc]
kernel:  svc_recv+0x1a4/0x520 [sunrpc]
kernel:  lockd+0x154/0x298 [lockd]
kernel:  kthread+0x2f8/0x398
kernel:  ret_from_fork+0x10/0x20
kernel: ---[ end trace 0000000000000000 ]---
---
 fs/lockd/svclock.c | 1 +
 1 file changed, 1 insertion(+)
fs/lockd/svclock.c +0 -0
index a31dc9588eb8..1dd0fec186de 100644

fs/lockd/svclock.c nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · line 658

Quoted 7 lines
fs/lockd/svclock.c +1 -0
@@ -652,6 +652,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
 	conflock->fl.c.flc_type = lock->fl.c.flc_type;
 	conflock->fl.fl_start = lock->fl.fl_start;
 	conflock->fl.fl_end = lock->fl.fl_end;
+	conflock->fl.fl_lmops = NULL;
 	locks_release_private(&lock->fl);
 
 	ret = nlm_lck_denied;

The problem sounds real, but I'm not sure I like this solution.

Quoted 6 lines

It seems like this is gaming the refcounting such that we take a reference in locks_copy_conflock() but then you zero out fl_lmops before that reference can be put.

Doesn't that mean that the real bug is that we're missing taking an owner reference in some case?

Quoted 9 lines

I had a bit of a look and .... and first it looked really strange that vfs_test_lock() uses the "fl" arg as both an "in" and an "out" - that is bound to cause refcounting issues.

But then I thought ... "no, wait" As an "in" parameter, "fl" is just a byte-range. fl_start and fl_end are all that matter. locks_overlap() uses these and nothing else of significance matters. In particular fl_owner doesn't and can't matter.

I think flc_owner does matter here. This is a TESTLCK operation. The entity requesting it is relevant.

Quoted 5 lines

So why, e.g., does nfsd4_lockt() set file_lock->c.flc_owner at all?

And nlm4svc_retrieve_args() shouldn't be setting up "the missing parts of the file_lock structure" for TEST_LOCK - should it?

Quoted 9 lines

I think we need to treat get-lock quite differently from set-lock. After all vfs_lock_file() does have a separate "conf" arg, while vfs_test_lock() doesn't. That is an important difference.

Maybe we should change vfs_test_lock() to be passed
filp, start, len, *conf

and not pretend that we are passing in a lock description??

I had the same thought. I think that we should aim to fix this with a small patch that can be easily backported first however, and then do that sort of cleanup on top.

Signature · 1 line
Jeff Layton <[email protected]>