87c3ef
From cddd253c5e3f0a7c3b91c35cea8ad1921cb43b98 Mon Sep 17 00:00:00 2001
87c3ef
From: Kinglong Mee <kinglongmee@gmail.com>
87c3ef
Date: Thu, 18 Jul 2019 11:43:01 +0800
87c3ef
Subject: [PATCH 454/456] features/locks: avoid use after freed of frame for
87c3ef
 blocked lock
87c3ef
87c3ef
The fop contains blocked lock may use freed frame info when other
87c3ef
unlock fop has unwind the blocked lock.
87c3ef
87c3ef
Because the blocked lock is added to block list in inode lock(or
87c3ef
other lock), after that, when out of the inode lock, the fop
87c3ef
contains the blocked lock should not use it.
87c3ef
87c3ef
Upstream Patch - https://review.gluster.org/#/c/glusterfs/+/23155/
87c3ef
87c3ef
>Change-Id: Icb309a1cc78380dc982b26d50c18d67e4f2c8915
87c3ef
>fixes: bz#1737291
87c3ef
>Signed-off-by: Kinglong Mee <mijinlong@horiscale.com>
87c3ef
87c3ef
Change-Id: Icb309a1cc78380dc982b26d50c18d67e4f2c8915
87c3ef
BUG: 1812789
87c3ef
Reviewed-on: https://code.engineering.redhat.com/gerrit/206465
87c3ef
Tested-by: RHGS Build Bot <nigelb@redhat.com>
87c3ef
Reviewed-by: Xavi Hernandez Juan <xhernandez@redhat.com>
87c3ef
---
87c3ef
 xlators/features/locks/src/common.c    | 4 ++++
87c3ef
 xlators/features/locks/src/entrylk.c   | 4 ++--
87c3ef
 xlators/features/locks/src/inodelk.c   | 7 +++++--
87c3ef
 xlators/features/locks/src/posix.c     | 5 +++--
87c3ef
 xlators/features/locks/src/reservelk.c | 2 --
87c3ef
 5 files changed, 14 insertions(+), 8 deletions(-)
87c3ef
87c3ef
diff --git a/xlators/features/locks/src/common.c b/xlators/features/locks/src/common.c
87c3ef
index 6e7fb4b..1406e70 100644
87c3ef
--- a/xlators/features/locks/src/common.c
87c3ef
+++ b/xlators/features/locks/src/common.c
87c3ef
@@ -1080,6 +1080,10 @@ pl_setlk(xlator_t *this, pl_inode_t *pl_inode, posix_lock_t *lock,
87c3ef
                    lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
87c3ef
                    lock->client_pid, lkowner_utoa(&lock->owner),
87c3ef
                    lock->user_flock.l_start, lock->user_flock.l_len);
87c3ef
+
87c3ef
+            pl_trace_block(this, lock->frame, NULL, NULL, F_SETLKW,
87c3ef
+                           &lock->user_flock, NULL);
87c3ef
+
87c3ef
             lock->blocked = 1;
87c3ef
             __insert_lock(pl_inode, lock);
87c3ef
             ret = -1;
87c3ef
diff --git a/xlators/features/locks/src/entrylk.c b/xlators/features/locks/src/entrylk.c
87c3ef
index ced5eca..93c649c 100644
87c3ef
--- a/xlators/features/locks/src/entrylk.c
87c3ef
+++ b/xlators/features/locks/src/entrylk.c
87c3ef
@@ -552,6 +552,8 @@ __lock_blocked_add(xlator_t *this, pl_inode_t *pinode, pl_dom_list_t *dom,
87c3ef
     gf_msg_trace(this->name, 0, "Blocking lock: {pinode=%p, basename=%s}",
87c3ef
                  pinode, lock->basename);
87c3ef
 
87c3ef
+    entrylk_trace_block(this, lock->frame, NULL, NULL, NULL, lock->basename,
87c3ef
+                        ENTRYLK_LOCK, lock->type);
87c3ef
 out:
87c3ef
     return -EAGAIN;
87c3ef
 }
87c3ef
@@ -932,8 +934,6 @@ out:
87c3ef
                           op_ret, op_errno);
87c3ef
     unwind:
87c3ef
         STACK_UNWIND_STRICT(entrylk, frame, op_ret, op_errno, NULL);
87c3ef
-    } else {
87c3ef
-        entrylk_trace_block(this, frame, volume, fd, loc, basename, cmd, type);
87c3ef
     }
87c3ef
 
87c3ef
     if (pcontend != NULL) {
87c3ef
diff --git a/xlators/features/locks/src/inodelk.c b/xlators/features/locks/src/inodelk.c
87c3ef
index a9c42f1..24dee49 100644
87c3ef
--- a/xlators/features/locks/src/inodelk.c
87c3ef
+++ b/xlators/features/locks/src/inodelk.c
87c3ef
@@ -420,6 +420,8 @@ __lock_blocked_add(xlator_t *this, pl_dom_list_t *dom, pl_inode_lock_t *lock,
87c3ef
                  lkowner_utoa(&lock->owner), lock->user_flock.l_start,
87c3ef
                  lock->user_flock.l_len);
87c3ef
 
87c3ef
+    pl_trace_block(this, lock->frame, NULL, NULL, F_SETLKW, &lock->user_flock,
87c3ef
+                   lock->volume);
87c3ef
 out:
87c3ef
     return -EAGAIN;
87c3ef
 }
87c3ef
@@ -959,6 +961,7 @@ pl_common_inodelk(call_frame_t *frame, xlator_t *this, const char *volume,
87c3ef
     int ret = -1;
87c3ef
     GF_UNUSED int dict_ret = -1;
87c3ef
     int can_block = 0;
87c3ef
+    short lock_type = 0;
87c3ef
     pl_inode_t *pinode = NULL;
87c3ef
     pl_inode_lock_t *reqlock = NULL;
87c3ef
     pl_dom_list_t *dom = NULL;
87c3ef
@@ -1024,13 +1027,13 @@ pl_common_inodelk(call_frame_t *frame, xlator_t *this, const char *volume,
87c3ef
             /* fall through */
87c3ef
 
87c3ef
         case F_SETLK:
87c3ef
+            lock_type = flock->l_type;
87c3ef
             memcpy(&reqlock->user_flock, flock, sizeof(struct gf_flock));
87c3ef
             ret = pl_inode_setlk(this, ctx, pinode, reqlock, can_block, dom,
87c3ef
                                  inode);
87c3ef
 
87c3ef
             if (ret < 0) {
87c3ef
-                if ((can_block) && (F_UNLCK != flock->l_type)) {
87c3ef
-                    pl_trace_block(this, frame, fd, loc, cmd, flock, volume);
87c3ef
+                if ((can_block) && (F_UNLCK != lock_type)) {
87c3ef
                     goto out;
87c3ef
                 }
87c3ef
                 gf_log(this->name, GF_LOG_TRACE, "returning EAGAIN");
87c3ef
diff --git a/xlators/features/locks/src/posix.c b/xlators/features/locks/src/posix.c
87c3ef
index 50f1265..7887b82 100644
87c3ef
--- a/xlators/features/locks/src/posix.c
87c3ef
+++ b/xlators/features/locks/src/posix.c
87c3ef
@@ -2557,6 +2557,7 @@ pl_lk(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t cmd,
87c3ef
     uint32_t lk_flags = 0;
87c3ef
     posix_locks_private_t *priv = this->private;
87c3ef
     pl_local_t *local = NULL;
87c3ef
+    short lock_type = 0;
87c3ef
 
87c3ef
     int ret = dict_get_uint32(xdata, GF_LOCK_MODE, &lk_flags);
87c3ef
     if (ret == 0) {
87c3ef
@@ -2701,6 +2702,7 @@ pl_lk(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t cmd,
87c3ef
         case F_SETLK:
87c3ef
             reqlock->frame = frame;
87c3ef
             reqlock->this = this;
87c3ef
+            lock_type = flock->l_type;
87c3ef
 
87c3ef
             pthread_mutex_lock(&pl_inode->mutex);
87c3ef
             {
87c3ef
@@ -2738,8 +2740,7 @@ pl_lk(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t cmd,
87c3ef
 
87c3ef
             ret = pl_setlk(this, pl_inode, reqlock, can_block);
87c3ef
             if (ret == -1) {
87c3ef
-                if ((can_block) && (F_UNLCK != flock->l_type)) {
87c3ef
-                    pl_trace_block(this, frame, fd, NULL, cmd, flock, NULL);
87c3ef
+                if ((can_block) && (F_UNLCK != lock_type)) {
87c3ef
                     goto out;
87c3ef
                 }
87c3ef
                 gf_log(this->name, GF_LOG_DEBUG, "returning EAGAIN");
87c3ef
diff --git a/xlators/features/locks/src/reservelk.c b/xlators/features/locks/src/reservelk.c
87c3ef
index 51076d7..604691f 100644
87c3ef
--- a/xlators/features/locks/src/reservelk.c
87c3ef
+++ b/xlators/features/locks/src/reservelk.c
87c3ef
@@ -312,8 +312,6 @@ grant_blocked_lock_calls(xlator_t *this, pl_inode_t *pl_inode)
87c3ef
         ret = pl_setlk(this, pl_inode, lock, can_block);
87c3ef
         if (ret == -1) {
87c3ef
             if (can_block) {
87c3ef
-                pl_trace_block(this, lock->frame, fd, NULL, cmd,
87c3ef
-                               &lock->user_flock, NULL);
87c3ef
                 continue;
87c3ef
             } else {
87c3ef
                 gf_log(this->name, GF_LOG_DEBUG, "returning EAGAIN");
87c3ef
-- 
87c3ef
1.8.3.1
87c3ef