ocfs2: avoid access invalid address when read o2dlm debug messages
authorYiwen Jiang <jiangyiwen@huawei.com>
Fri, 4 Sep 2015 22:44:37 +0000 (15:44 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 4 Sep 2015 23:54:41 +0000 (16:54 -0700)
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>
fs/ocfs2/dlm/dlmmaster.c
fs/ocfs2/dlm/dlmthread.c

index fdf4b41..46b8b2b 100644 (file)
@@ -498,16 +498,6 @@ static void dlm_lockres_release(struct kref *kref)
        mlog(0, "destroying lockres %.*s\n", res->lockname.len,
             res->lockname.name);
 
-       spin_lock(&dlm->track_lock);
-       if (!list_empty(&res->tracking))
-               list_del_init(&res->tracking);
-       else {
-               mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
-                    res->lockname.len, res->lockname.name);
-               dlm_print_one_lock_resource(res);
-       }
-       spin_unlock(&dlm->track_lock);
-
        atomic_dec(&dlm->res_cur_count);
 
        if (!hlist_unhashed(&res->hash_node) ||
@@ -795,8 +785,18 @@ lookup:
                dlm_lockres_grab_inflight_ref(dlm, tmpres);
 
                spin_unlock(&tmpres->spinlock);
-               if (res)
+               if (res) {
+                       spin_lock(&dlm->track_lock);
+                       if (!list_empty(&res->tracking))
+                               list_del_init(&res->tracking);
+                       else
+                               mlog(ML_ERROR, "Resource %.*s not "
+                                               "on the Tracking list\n",
+                                               res->lockname.len,
+                                               res->lockname.name);
+                       spin_unlock(&dlm->track_lock);
                        dlm_lockres_put(res);
+               }
                res = tmpres;
                goto leave;
        }
index 69aac6f..2e5e6d5 100644 (file)
@@ -211,6 +211,16 @@ static void dlm_purge_lockres(struct dlm_ctxt *dlm,
 
        __dlm_unhash_lockres(dlm, res);
 
+       spin_lock(&dlm->track_lock);
+       if (!list_empty(&res->tracking))
+               list_del_init(&res->tracking);
+       else {
+               mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
+                               res->lockname.len, res->lockname.name);
+               __dlm_print_one_lock_resource(res);
+       }
+       spin_unlock(&dlm->track_lock);
+
        /* lockres is not in the hash now.  drop the flag and wake up
         * any processes waiting in dlm_get_lock_resource. */
        if (!master) {