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