Skip to contentSkip to conversation

lockd: make the nlm_file reference count atomic

PATCH Superseded Benjamin Coddington 2 participants 3 messages Oct 2

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lockd: make the nlm_file reference count atomic

nlmsvc_create_block() takes its reference on the nlm_file with file->f_count++ while holding only that file's f_mutex. Every other update of f_count is made under nlm_file_mutex, and can come from another task: nlm_traverse_files() pins each file it visits and runs from lockd_down() in any network namespace and from nfsd's unlock_ip and unlock_filesystem, and nlm_release_file() runs from the release of a GRANTED callback. An update can be lost, and the count then reaches zero while a reference is still held:

  BUG: KASAN: slab-use-after-free in nlm_release_file+0x504/0x7e8 [lockd]
  Read of size 4 at addr ffff0000a738f6c4 by task lockd/11494

  Call trace:
   nlm_release_file+0x504/0x7e8 [lockd]
   nlm4svc_do_lock+0x2bc/0x468 [lockd]
   nlm4svc_proc_lock+0x1c/0x30 [lockd]
   nlmsvc_dispatch+0xa8/0x200 [lockd]
   svc_process_common+0xb08/0x1b80 [sunrpc]
   svc_process+0x424/0xb70 [sunrpc]
   svc_recv+0x1680/0x2408 [sunrpc]
   lockd+0x148/0x288 [lockd]

  Allocated by task 10651:
   nlm_lookup_file+0x2a4/0x5c0 [lockd]
   nlm4svc_lookup_file.constprop.0+0x4dc/0x848 [lockd]
   nlm4svc_do_lock+0x284/0x468 [lockd]
   nlm4svc_proc_lock+0x1c/0x30 [lockd]

  Freed by task 11494:
   nlm_release_file+0x46c/0x7e8 [lockd]
   nlmsvc_free_block+0x1a4/0x298 [lockd]
   nlmsvc_lock+0x2cc/0x1a80 [lockd]
   nlm4svc_do_lock+0x324/0x468 [lockd]
   nlm4svc_proc_lock+0x1c/0x30 [lockd]

That is v7.2 with commit 526c49cff3f7 ("lockd: pin next file across nlm_inspect_file lock-drop") applied, with NLM lock and unlock traffic in one network namespace while a second namespace mounts and unmounts NFSv3, which shuts lockd down in that namespace each time.

Make f_count an atomic_t. Files are still only freed under nlm_file_mutex, and nlmsvc_create_block() increments a count that its caller already holds. It is an atomic_t rather than a refcount_t because the count rests at zero while only locks hold the file in the table.

Fixes d9f6eb75d490 ("lockd: blocks should hold a reference to the nlm_file")
Cc <[email protected]> # 526c49cff3f7: lockd: pin next file across nlm_inspect_file lock-drop
---
 fs/lockd/lockd.h   |  2 +-
 fs/lockd/svclock.c |  2 +-
 fs/lockd/svcsubs.c | 18 ++++++++++--------
 3 files changed, 12 insertions(+), 10 deletions(-)
fs/lockd/lockd.h +1 -1
index 14cc952fe81a..55958e490ee4 100644
@@ -201,7 +201,7 @@ struct nlm_file {
 	struct lockd_share *	f_shares;	/* DOS shares */
 	struct list_head	f_blocks;	/* blocked locks */
 	unsigned int		f_locks;	/* guesstimate # of locks */
-	unsigned int		f_count;	/* reference count */
+	atomic_t		f_count;	/* reference count */
 	struct mutex		f_mutex;	/* avoid concurrent access */
 };
 
fs/lockd/svclock.c +1 -1
index 495eacb3264f..0a6540409ef8 100644
@@ -221,7 +221,7 @@ nlmsvc_create_block(struct svc_rqst *rqstp, struct nlm_host *host,
 	block->b_daemon = rqstp->rq_server;
 	block->b_host   = host;
 	block->b_file   = file;
-	file->f_count++;
+	atomic_inc(&file->f_count);
 
 	/* Add to file's list of blocks */
 	list_add(&block->b_flist, &file->f_blocks);
fs/lockd/svcsubs.c +10 -8
index ffab76278bb2..147852e8c3be 100644
@@ -186,9 +186,10 @@ nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result,
 	hlist_add_head(&file->f_list, &nlm_files[hash]);
 
 found:
-	dprintk("lockd: found file %p (count %d)\n", file, file->f_count);
+	dprintk("lockd: found file %p (count %d)\n", file,
+		atomic_read(&file->f_count));
 	*result = file;
-	file->f_count++;
+	atomic_inc(&file->f_count);
 
 out_unlock:
 	mutex_unlock(&nlm_file_mutex);
@@ -333,7 +334,8 @@ nlm_file_inuse(struct nlm_file *file)
 	struct file_lock *fl;
 	struct file_lock_context *flctx = locks_inode_context(inode);
 
-	if (file->f_count || !list_empty(&file->f_blocks) || file->f_shares)
+	if (atomic_read(&file->f_count) || !list_empty(&file->f_blocks) ||
+	    file->f_shares)
 		return 1;
 
 	if (flctx && !list_empty_careful(&flctx->flc_posix)) {
@@ -371,7 +373,7 @@ nlm_traverse_files(void *data, nlm_host_match_fn_t match,
 		file = hlist_entry_safe(nlm_files[i].first,
 					struct nlm_file, f_list);
 		if (file)
-			file->f_count++;
+			atomic_inc(&file->f_count);
 		while (file) {
 			/*
 			 * Pin the next neighbour before we drop the mutex
@@ -384,7 +386,7 @@ nlm_traverse_files(void *data, nlm_host_match_fn_t match,
 			next = hlist_entry_safe(file->f_list.next,
 						struct nlm_file, f_list);
 			if (next)
-				next->f_count++;
+				atomic_inc(&next->f_count);
 
 			if (!is_failover_file || is_failover_file(data, file)) {
 				mutex_unlock(&nlm_file_mutex);
@@ -395,7 +397,7 @@ nlm_traverse_files(void *data, nlm_host_match_fn_t match,
 				mutex_lock(&nlm_file_mutex);
 			}
 
-			file->f_count--;
+			atomic_dec(&file->f_count);
 			nlm_file_release(file);
 			file = next;
 		}
@@ -417,13 +419,13 @@ void
 nlm_release_file(struct nlm_file *file)
 {
 	dprintk("lockd: nlm_release_file(%p, ct = %d)\n",
-				file, file->f_count);
+				file, atomic_read(&file->f_count));
 
 	/* Lock file table */
 	mutex_lock(&nlm_file_mutex);
 
 	/* If there are no more locks etc, delete the file */
-	if (--file->f_count == 0 && !nlm_file_inuse(file))
+	if (atomic_dec_and_test(&file->f_count) && !nlm_file_inuse(file))
 		nlm_delete_file(file);
 
 	mutex_unlock(&nlm_file_mutex);
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 46 lines · Benjamin Coddington

nlmsvc_create_block() takes its reference on the nlm_file with file->f_count++ while holding only that file's f_mutex. Every other update of f_count is made under nlm_file_mutex, and can come from another task: nlm_traverse_files() pins each file it visits and runs from lockd_down() in any network namespace and from nfsd's unlock_ip and unlock_filesystem, and nlm_release_file() runs from the release of a GRANTED callback. An update can be lost, and the count then reaches zero while a reference is still held:

  BUG: KASAN: slab-use-after-free in nlm_release_file+0x504/0x7e8 [lockd]
  Read of size 4 at addr ffff0000a738f6c4 by task lockd/11494

  Call trace:
   nlm_release_file+0x504/0x7e8 [lockd]
   nlm4svc_do_lock+0x2bc/0x468 [lockd]
   nlm4svc_proc_lock+0x1c/0x30 [lockd]
   nlmsvc_dispatch+0xa8/0x200 [lockd]
   svc_process_common+0xb08/0x1b80 [sunrpc]
   svc_process+0x424/0xb70 [sunrpc]
   svc_recv+0x1680/0x2408 [sunrpc]
   lockd+0x148/0x288 [lockd]

  Allocated by task 10651:
   nlm_lookup_file+0x2a4/0x5c0 [lockd]
   nlm4svc_lookup_file.constprop.0+0x4dc/0x848 [lockd]
   nlm4svc_do_lock+0x284/0x468 [lockd]
   nlm4svc_proc_lock+0x1c/0x30 [lockd]

  Freed by task 11494:
   nlm_release_file+0x46c/0x7e8 [lockd]
   nlmsvc_free_block+0x1a4/0x298 [lockd]
   nlmsvc_lock+0x2cc/0x1a80 [lockd]
   nlm4svc_do_lock+0x324/0x468 [lockd]
   nlm4svc_proc_lock+0x1c/0x30 [lockd]

That is v7.2 with commit 526c49cff3f7 ("lockd: pin next file across nlm_inspect_file lock-drop") applied, with NLM lock and unlock traffic in one network namespace while a second namespace mounts and unmounts NFSv3, which shuts lockd down in that namespace each time.

Make f_count an atomic_t. Files are still only freed under nlm_file_mutex, and nlmsvc_create_block() increments a count that its caller already holds. It is an atomic_t rather than a refcount_t because the count rests at zero while only locks hold the file in the table.

Quoted 4 lines
Fixes d9f6eb75d490 ("lockd: blocks should hold a reference to the

nlm_file")

Cc <[email protected]> # 526c49cff3f7: lockd: pin next file across nlm_inspect_file lock-drop

checkpatch reports:

WARNING: From:/Signed-off-by: email address mismatch: 'From: Benjamin Coddington <[email protected]>' != 'Signed-off-by: Benjamin Coddington <[email protected]>'

Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)
#2 lore raw
Quoted 47 lines · Chuck Lever
Quoted 46 lines · Benjamin Coddington

nlmsvc_create_block() takes its reference on the nlm_file with file->f_count++ while holding only that file's f_mutex. Every other update of f_count is made under nlm_file_mutex, and can come from another task: nlm_traverse_files() pins each file it visits and runs from lockd_down() in any network namespace and from nfsd's unlock_ip and unlock_filesystem, and nlm_release_file() runs from the release of a GRANTED callback. An update can be lost, and the count then reaches zero while a reference is still held:

  BUG: KASAN: slab-use-after-free in nlm_release_file+0x504/0x7e8 [lockd]
  Read of size 4 at addr ffff0000a738f6c4 by task lockd/11494

  Call trace:
   nlm_release_file+0x504/0x7e8 [lockd]
   nlm4svc_do_lock+0x2bc/0x468 [lockd]
   nlm4svc_proc_lock+0x1c/0x30 [lockd]
   nlmsvc_dispatch+0xa8/0x200 [lockd]
   svc_process_common+0xb08/0x1b80 [sunrpc]
   svc_process+0x424/0xb70 [sunrpc]
   svc_recv+0x1680/0x2408 [sunrpc]
   lockd+0x148/0x288 [lockd]

  Allocated by task 10651:
   nlm_lookup_file+0x2a4/0x5c0 [lockd]
   nlm4svc_lookup_file.constprop.0+0x4dc/0x848 [lockd]
   nlm4svc_do_lock+0x284/0x468 [lockd]
   nlm4svc_proc_lock+0x1c/0x30 [lockd]

  Freed by task 11494:
   nlm_release_file+0x46c/0x7e8 [lockd]
   nlmsvc_free_block+0x1a4/0x298 [lockd]
   nlmsvc_lock+0x2cc/0x1a80 [lockd]
   nlm4svc_do_lock+0x324/0x468 [lockd]
   nlm4svc_proc_lock+0x1c/0x30 [lockd]

That is v7.2 with commit 526c49cff3f7 ("lockd: pin next file across nlm_inspect_file lock-drop") applied, with NLM lock and unlock traffic in one network namespace while a second namespace mounts and unmounts NFSv3, which shuts lockd down in that namespace each time.

Make f_count an atomic_t. Files are still only freed under nlm_file_mutex, and nlmsvc_create_block() increments a count that its caller already holds. It is an atomic_t rather than a refcount_t because the count rests at zero while only locks hold the file in the table.

Quoted 8 lines
Quoted 4 lines
Fixes d9f6eb75d490 ("lockd: blocks should hold a reference to the

nlm_file")

Cc <[email protected]> # 526c49cff3f7: lockd: pin next file across nlm_inspect_file lock-drop

checkpatch reports:

WARNING: From:/Signed-off-by: email address mismatch: 'From: Benjamin Coddington <[email protected]>' != 'Signed-off-by: Benjamin Coddington <[email protected]>'

Checkpatch levels up, and my mail gateway rewrites my from address. I'll fix up my mess over here and send a v2.

Ben