Skip to content

Commit f57a22d

Browse files
jiangyiwen123torvalds
authored andcommitted
ocfs2: avoid access invalid address when read o2dlm debug messages
The following case will lead to a lockres is freed but is still in use. cat /sys/kernel/debug/o2dlm/locking_state dlm_thread lockres_seq_start -> lock dlm->track_lock -> get resA resA->refs decrease to 0, call dlm_lockres_release, and wait for "cat" unlock. Although resA->refs is already set to 0, increase resA->refs, and then unlock lock dlm->track_lock -> list_del_init() -> unlock -> free resA In such a race case, invalid address access may occurs. So we should delete list res->tracking before resA->refs decrease to 0. Signed-off-by: Yiwen Jiang <jiangyiwen@huawei.com> Reviewed-by: Joseph Qi <joseph.qi@huawei.com> Cc: Joel Becker <jlbec@evilplan.org> Signed-off-by: Mark Fasheh <mfasheh@suse.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 743b5f1 commit f57a22d

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

fs/ocfs2/dlm/dlmmaster.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -498,16 +498,6 @@ static void dlm_lockres_release(struct kref *kref)
498498
mlog(0, "destroying lockres %.*s\n", res->lockname.len,
499499
res->lockname.name);
500500

501-
spin_lock(&dlm->track_lock);
502-
if (!list_empty(&res->tracking))
503-
list_del_init(&res->tracking);
504-
else {
505-
mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
506-
res->lockname.len, res->lockname.name);
507-
dlm_print_one_lock_resource(res);
508-
}
509-
spin_unlock(&dlm->track_lock);
510-
511501
atomic_dec(&dlm->res_cur_count);
512502

513503
if (!hlist_unhashed(&res->hash_node) ||
@@ -795,8 +785,18 @@ struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
795785
dlm_lockres_grab_inflight_ref(dlm, tmpres);
796786

797787
spin_unlock(&tmpres->spinlock);
798-
if (res)
788+
if (res) {
789+
spin_lock(&dlm->track_lock);
790+
if (!list_empty(&res->tracking))
791+
list_del_init(&res->tracking);
792+
else
793+
mlog(ML_ERROR, "Resource %.*s not "
794+
"on the Tracking list\n",
795+
res->lockname.len,
796+
res->lockname.name);
797+
spin_unlock(&dlm->track_lock);
799798
dlm_lockres_put(res);
799+
}
800800
res = tmpres;
801801
goto leave;
802802
}

fs/ocfs2/dlm/dlmthread.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,16 @@ static void dlm_purge_lockres(struct dlm_ctxt *dlm,
211211

212212
__dlm_unhash_lockres(dlm, res);
213213

214+
spin_lock(&dlm->track_lock);
215+
if (!list_empty(&res->tracking))
216+
list_del_init(&res->tracking);
217+
else {
218+
mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
219+
res->lockname.len, res->lockname.name);
220+
__dlm_print_one_lock_resource(res);
221+
}
222+
spin_unlock(&dlm->track_lock);
223+
214224
/* lockres is not in the hash now. drop the flag and wake up
215225
* any processes waiting in dlm_get_lock_resource. */
216226
if (!master) {

0 commit comments

Comments
 (0)