dlm: validate lockspace under ls_lock in dlm_release_lockspace()
PATCH 1/1 dlm: validate lockspace under ls_lock in dlm_release_lockspace()
dlm_release_lockspace() currently calls dlm_find_lockspace_local(), which dereferences the raw lockspace pointer to increment ls_count, and immediately calls dlm_put_lockspace() before acquiring ls_lock.
When two callers concurrently release the same lockspace (for example via concurrent DLM_USER_REMOVE_LOCKSPACE requests through device_remove_lockspace(), which also drops its lookup reference before calling dlm_release_lockspace()), the first caller can enter release_lockspace(), remove the lockspace from lslist, and queue ls_free_work to free ls before the second caller dereferences ls in dlm_find_lockspace_local() or release_lockspace().
Since lockspaces are only added to and removed from lslist while holding ls_lock, move the lockspace lookup inside ls_lock and verify that lockspace is still present on lslist under lslist_lock before calling release_lockspace(). If it has already been removed, return -EINVAL without dereferencing the pointer.
---
Found by code inspection. Compile-tested on x86_64 with W=1 and KASAN (fs/dlm/lockspace.o); not runtime-tested on a live DLM cluster.
fs/dlm/lockspace.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
fs/dlm/lockspace.c +11 -5
@@ -800,12 +800,18 @@ int dlm_release_lockspace(void *lockspace, unsigned int release_option)if (release_option > __DLM_RELEASE_MAX)return -EINVAL ; - ls =dlm_find_lockspace_local (lockspace); -if (!ls) -return -EINVAL ; -dlm_put_lockspace (ls); -mutex_lock (&ls_lock); +spin_lock_bh (&lslist_lock); +list_for_each_entry (ls, &lslist, ls_list) { +if (ls == lockspace) + goto found; + } +spin_unlock_bh (&lslist_lock); +mutex_unlock (&ls_lock); +return -EINVAL ; + +found: +spin_unlock_bh (&lslist_lock); error =release_lockspace (ls, release_option);if (!error) ls_count--;
Signature · 1 line
2.43.0