dlm: various fixes
Alexander Aring
maintainercover dlm: various fixes
Hi David,
This patch series contains mostly AI-assisted fixes and cleanups across the DLM subsystem:
- Add missing length checks for oversized inner DLM messages, this prevents potential buffer overruns or malformed packet handling.
- Enforce RSB assignment before using LKBs, this ensures LKBs cannot exist in lkbxa without an associated RSB set. It resolves an issue with concurrent DLM user requests.
- Prevent DLM_ASSERT() on canceled pending requests, this fixes an assertion failure triggered when a initial request is canceled and recovery is triggered during this state.
- Fix lockspace node memory leak during release, this resolves a leak involving delayed member removal during active lockspace releasing members. Active releasing members will not run recovery so the delayed cleanup of removed of members will never occur.
Ensuring that LKBs always have an RSB assigned should also reduce the likelihood of subtle bugs elsewhere in the codebase where an RSB presence check might be missing.
Checkpatch threw a warning for the "Assisted-by:" tag. It seems checkpatch can't recognize this yet?
There is a pending sysfs fix [0] that conflicts with [1]. The fix for
[1] does not go through the dlm tree so [0] is delayed until [1] is available.
- Alex
[0] https://lore.kernel.org/gfs2/[email protected]/
[1] https://lore.kernel.org/gfs2/[email protected]/
Alexander Aring (1):
dlm: fix memory leak on active leaving member
Ameer Hamza (1):
dlm: don't set RSB_MASTER_UNCERTAIN on rsb with no master
Jérémy Jean (1):
dlm: reject wrapped messages with oversized inner lengths
Yogesh Gaur (1):
dlm: don't publish a lkb in ls_lkbxa before it has an rsb
fs/dlm/config.c | 9 +++++ fs/dlm/lock.c | 98 ++++++++++++++++++++++++++++++++--------------- fs/dlm/midcomms.c | 8 ++++ 3 files changed, 85 insertions(+), 30 deletions(-)
Signature · 1 line
2.43.0
Alexander Aring
maintainerPATCH RESEND 1/4 dlm: reject wrapped messages with oversized inner lengths
From: Jérémy Jean <[email protected]>
Reject DLM_OPTS packets whose wrapped inner message length exceeds the bytes remaining inside the validated outer packet. This keeps malformed wrapped RCOM replies from reaching downstream length-driven copies.
--- fs/dlm/midcomms.c | 8 ++++++++ 1 file changed, 8 insertions(+)
fs/dlm/midcomms.c +8 -0
@@ -652,6 +652,14 @@ static int dlm_opts_check_msglen(const union dlm_packet *p, uint16_t msglen,return -1 ; } + p = (const union dlm_packet *)((const unsigned char *)p->opts.o_opts + +le16_to_cpu (p->opts.o_optlen)); +if (le16_to_cpu (p->header.h_length) > len) { +log_print ("inner msg too large: %u > %d, will skip this message from node %d" , +le16_to_cpu (p->header.h_length), len, nodeid); +return -1 ; + } +return 0 ; }
Signature · 1 line
2.43.0
Alexander Aring
maintainerPATCH RESEND 3/4 dlm: don't set RSB_MASTER_UNCERTAIN on rsb with no master
In reply to Alexander AringFrom: Ameer Hamza <[email protected]>
When a local request reuses an inactive rsb, find_rsb_dir() sets RSB_MASTER_UNCERTAIN if res_master_nodeid is not our nodeid. This also matches an rsb that never got a master (res_master_nodeid 0, res_nodeid -1). set_master() then trusts the "cached" master, copies res_nodeid -1 into lkb_nodeid and skips the lookup, and _request_lock() hits the DLM_ASSERT(r->res_nodeid >= 0) in is_remote():
rsb: nodeid -1 master 0 dir 1 flags 200 first 1 rlc 0 name pr
Kernel panic - not syncing: DLM: Record message above and reboot. Call Trace: _request_lock.cold+0x3e/0x3e [dlm] request_lock+0xcc/0x110 [dlm] dlm_user_request+0x110/0x340 [dlm] device_write+0x605/0x6d0 [dlm]
Such an rsb is left behind when a request is cancelled while its lookup is still outstanding and the lockspace then recovers, e.g. because the dir node died: dlm_recover_waiters_post() completes the cancelled lkb instead of resending it, so no master is ever set. Once the rsb is inactive, the next request for the name revives it and crashes as above. If we became the dir node in that recovery, deactivate_rsb() even keeps the rsb as a dir record, so it does not age out.
Before commit c04fecb4d9f7 ("dlm: use rsbtbl as resource directory") the flag was only set when res_nodeid was > 0. Restore that and set the flag only when a master is actually cached, so that set_master() does a normal lookup (or becomes the master itself as dir node) for an rsb that has none. res_first_lkid is still cleared, as the cancelled lkb leaves a stale id behind.
--- fs/dlm/lock.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
fs/dlm/lock.c +6 -2
@@ -826,8 +826,12 @@ static int find_rsb_dir(struct dlm_ls *ls, const void *name, int len,if (from_local && (r->res_master_nodeid != our_nodeid)) {/* Because we have held no locks on this rsb, -res_master_nodeid could have become stale. */ -rsb_set_flag (r,RSB_MASTER_UNCERTAIN ); +* res_master_nodeid could have become stale. If no master +* was ever set, there is nothing to confirm and set_master() +* must resolve it as for a new rsb. +*/ +if (r->res_master_nodeid) +rsb_set_flag (r,RSB_MASTER_UNCERTAIN ); r->res_first_lkid =0 ; }
Signature · 1 line
2.43.0
Alexander Aring
maintainerPATCH RESEND 4/4 dlm: fix memory leak on active leaving member
In reply to Alexander AringWhen a node calls dlm_lockspace_release() locally, it does not subsequently run recovery. Commit de7b4869b4ec ("dlm: add new configfs entry release_recover for lockspace members") entry began tracking leaving members to perform optional post processing, such as invoking the slot callback. Because a locally leaving member does not execute recovery, it never invokes dlm_config_nodes(), leaving the allocated tracking entries uncleaned.
This issue was detected by kmemleak.
unreferenced object 0xffff88811d68bcc0 (size 32):
comm "dlm_controld", pid 406, jiffies 4294749992 hex dump (first 32 bytes):
03 00 00 00 00 00 00 00 08 bd 68 1d 81 88 ff ff ..........h.....
88 bc 68 1d 81 88 ff ff 00 00 00 00 00 00 00 00 ..h.............
backtrace (crc be1c20bb):
kmemleak_alloc+0x85/0xa0
__kmalloc_cache_noprof+0x2ac/0x380
drop_node+0x73/0x550
client_drop_item+0x83/0xe0
configfs_rmdir+0x735/0x910
vfs_rmdir+0x24a/0x820
filename_rmdir+0x3a3/0x4a0
__x64_sys_rmdir+0x42/0x60
x64_sys_call+0x1e8d/0x1fb0
do_syscall_64+0x87/0x490
entry_SYSCALL_64_after_hwframe+0x76/0x7eA local node executing dlm_lockspace_release() does not require additional recovery handling, such as invoking the slot callback. Fix this memory leak by freeing the resources allocated in drop_node() when the lockspace configfs entry is released.
--- fs/dlm/config.c | 9 +++++++++ 1 file changed, 9 insertions(+)
fs/dlm/config.c +9 -0
@@ -533,9 +533,18 @@ static struct config_group *make_space(struct config_group *g, const char *name)static void drop_space (struct config_group *g,struct config_item *i) {struct dlm_space *sp =config_item_to_space (i); +struct dlm_member_gone *mb_gone, *mb_safe;/* assert list_empty(&sp->members) */ +mutex_lock (&sp->members_lock); +list_for_each_entry_safe (mb_gone, mb_safe, &sp->members_gone, list) { +list_del (&mb_gone->list); + sp->members_gone_count--; +kfree (mb_gone); + } +mutex_unlock (&sp->members_lock); +configfs_remove_default_groups (&sp->group);config_item_put (i); }
Signature · 1 line
2.43.0
Alexander Aring
maintainerPATCH RESEND 2/4 dlm: don't publish a lkb in ls_lkbxa before it has an rsb
In reply to Alexander AringFrom: Yogesh Gaur <[email protected]>
_create_lkb() puts a new lkb into ls_lkbxa straight away, which is also what assigns its lkb_id, but the lkb only gets an rsb later, when request_lock(), receive_request(), dlm_recover_master_copy() or dlm_debug_add_lkb() call attach_lkb(). In between, find_lkb() hands the lkb out with lkb_resource still NULL, and its callers go straight for the rsb:
r = lkb->lkb_resource; hold_rsb(r); lock_rsb(r);
For the userspace API the lkid is simply whatever was written to the misc device, so a lkid can be aimed at a lkb that another thread is still building, and hold_rsb() reads res_flags off NULL:
BUG: KASAN: null-ptr-deref in rsb_flag fs/dlm/dlm_internal.h:386 [inline] BUG: KASAN: null-ptr-deref in hold_rsb fs/dlm/lock.c:334 [inline] BUG: KASAN: null-ptr-deref in unlock_lock fs/dlm/lock.c:3333 [inline] BUG: KASAN: null-ptr-deref in dlm_user_unlock+0x2ab/0x690 fs/dlm/lock.c:5956 Read of size 8 at addr 0000000000000050 by task syz.3.570/7893 rsb_flag fs/dlm/dlm_internal.h:386 [inline] hold_rsb fs/dlm/lock.c:334 [inline] unlock_lock fs/dlm/lock.c:3333 [inline] dlm_user_unlock+0x2ab/0x690 fs/dlm/lock.c:5956 device_user_unlock+0x1ca/0x260 fs/dlm/user.c:321
Make it impossible for ls_lkbxa to hold a lkb without an rsb, rather than filtering such lkbs in find_lkb() or reserving a NULL entry that every walker would have to know about. create_lkb() no longer touches ls_lkbxa; attach_lkb() sets lkb_resource and only then allocates the lkb_id and stores the lkb, so the lkb becomes visible already attached. dlm_debug_add_lkb() presets the id it wants and attach_lkb() allocates exactly that one.
attach_lkb() can now fail, and each of its four callers releases the lkb on the error path it already has for a failure just before the attach. On failure attach_lkb() also clears lkb_id, so that __put_lkb() cannot erase another lkb's entry when a requested id was busy. All four callers hold the rsb lock, and nothing takes an rsb lock under ls_lkbxa_lock, so taking it there adds no new lock ordering.
As a side effect an lkb has no lkb_id until it is attached, so trace_dlm_lock_start() and the validate_lock_args() and receive_rcom_lock_args() error messages report 0 for a new lock.
The unchecked r = lkb->lkb_resource goes back to the original DLM import, but an untrusted lkid only became possible once the userspace device interface was added, so that is the tag below.
--- fs/dlm/lock.c | 90 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 28 deletions(-)
fs/dlm/lock.c +62 -28
@@ -1488,10 +1488,38 @@ void free_inactive_rsb(struct dlm_rsb *r)/* Attaching/detaching lkb's from rsb's is for rsb reference counting. The rsb must exist as long as any lkb's for it do. */ -static void attach_lkb (struct dlm_rsb *r,struct dlm_lkb *lkb) +/* +* An lkb is only put into ls_lkbxa here, once it has an rsb, so +* find_lkb() never returns an lkb without one. The lkb_id is assigned +* here too; dlm_debug_add_lkb() presets the one it wants. +*/ +static int attach_lkb (struct dlm_rsb *r,struct dlm_lkb *lkb) { +struct dlm_ls *ls = r->res_ls; +struct xa_limit limit =XA_LIMIT (1 ,U32_MAX ); +int rv; + +if (lkb->lkb_id) + limit =XA_LIMIT (lkb->lkb_id, lkb->lkb_id); +hold_rsb (r); lkb->lkb_resource = r; + +write_lock_bh (&ls->ls_lkbxa_lock); + rv =xa_alloc (&ls->ls_lkbxa, &lkb->lkb_id, lkb, limit,GFP_ATOMIC ); +write_unlock_bh (&ls->ls_lkbxa_lock); + +if (rv <0 ) { +log_error (ls,"%s xa error %d" , __func__, rv); +/* the caller still holds its own rsb reference */ + lkb->lkb_resource =NULL ; +put_rsb (r); +/* don't let __put_lkb() erase another lkb's id */ + lkb->lkb_id =0 ; +return rv; + } + +return 0 ; }static void detach_lkb (struct dlm_lkb *lkb)
@@ -1502,15 +1530,9 @@ static void detach_lkb(struct dlm_lkb *lkb) } } -static int _create_lkb (struct dlm_ls *ls,struct dlm_lkb **lkb_ret, -unsigned long start,unsigned long end) +static int create_lkb (struct dlm_ls *ls,struct dlm_lkb **lkb_ret) { -struct xa_limit limit;struct dlm_lkb *lkb; -int rv; - - limit.max = end; - limit.min = start; lkb =dlm_allocate_lkb ();if (!lkb)
@@ -1525,25 +1547,10 @@ static int _create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret,INIT_LIST_HEAD (&lkb->lkb_ownqueue);INIT_LIST_HEAD (&lkb->lkb_rsb_lookup); -write_lock_bh (&ls->ls_lkbxa_lock); - rv =xa_alloc (&ls->ls_lkbxa, &lkb->lkb_id, lkb, limit,GFP_ATOMIC ); -write_unlock_bh (&ls->ls_lkbxa_lock); - -if (rv <0 ) { -log_error (ls,"create_lkb xa error %d" , rv); -dlm_free_lkb (lkb); -return rv; - } - *lkb_ret = lkb;return 0 ; } -static int create_lkb (struct dlm_ls *ls,struct dlm_lkb **lkb_ret) -{ -return _create_lkb (ls, lkb_ret,1 ,ULONG_MAX ); -} -static int find_lkb (struct dlm_ls *ls,uint32_t lkid,struct dlm_lkb **lkb_ret) {struct dlm_lkb *lkb;
@@ -3294,7 +3301,12 @@ static int request_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,lock_rsb (r); -attach_lkb (r, lkb); + error =attach_lkb (r, lkb); +if (error) { +unlock_rsb (r); +put_rsb (r); +return error; + } lkb->lkb_lksb->sb_lkid = lkb->lkb_id; error =_request_lock (r, lkb);
@@ -4038,7 +4050,14 @@ static int receive_request(struct dlm_ls *ls, const struct dlm_message *ms) } } -attach_lkb (r, lkb); + error =attach_lkb (r, lkb); +if (error) { +unlock_rsb (r); +put_rsb (r); +__put_lkb (ls, lkb); + goto fail; + } + error =do_request (r, lkb);send_request_reply (r, lkb, error);do_request_effects (r, lkb, error);
@@ -5642,7 +5661,12 @@ int dlm_recover_master_copy(struct dlm_ls *ls, const struct dlm_rcom *rc, goto out_unlock; } -attach_lkb (r, lkb); + error =attach_lkb (r, lkb); +if (error) { +__put_lkb (ls, lkb); + goto out_unlock; + } +add_lkb (r, lkb, rl->rl_status); ls->ls_recover_locks_in++;
@@ -6300,12 +6324,14 @@ int dlm_debug_add_lkb(struct dlm_ls *ls, uint32_t lkb_id, char *name, int len,if (!lksb)return -ENOMEM ; - error =_create_lkb (ls, &lkb, lkb_id, lkb_id +1 ); + error =create_lkb (ls, &lkb);if (error) {kfree (lksb);return error; } + lkb->lkb_id = lkb_id; +dlm_set_dflags_val (lkb, lkb_dflags); lkb->lkb_nodeid = lkb_nodeid; lkb->lkb_lksb = lksb;
@@ -6321,7 +6347,15 @@ int dlm_debug_add_lkb(struct dlm_ls *ls, uint32_t lkb_id, char *name, int len, }lock_rsb (r); -attach_lkb (r, lkb); + error =attach_lkb (r, lkb); +if (error) { +unlock_rsb (r); +put_rsb (r); +kfree (lksb); +__put_lkb (ls, lkb); +return error; + } +add_lkb (r, lkb, lkb_status);unlock_rsb (r);put_rsb (r);
Signature · 1 line
2.43.0