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