Blob Blame History Raw
From f114ba25fab57d1ab9a51fc1f101f2b5571f167a Mon Sep 17 00:00:00 2001
From: karthik-us <ksubrahm@redhat.com>
Date: Mon, 7 Jun 2021 19:24:55 +0530
Subject: [PATCH 583/584] protocol/client: Initialize list head to prevent NULL
 de-reference

> Upstream patch: https://github.com/gluster/glusterfs/pull/2456/commits/00761df0cd14833ff256b69dba7cf8e2b699554c
> fixes: #2443
> Change-Id: I86ef0270d41d6fb924db97fde3196d7c98c8b564
> Signed-off-by: Pranith Kumar K <pranith.karampuri@phonepe.com>

BUG: 1689375
Change-Id: I86ef0270d41d6fb924db97fde3196d7c98c8b564
Signed-off-by: karthik-us <ksubrahm@redhat.com>
Reviewed-on: https://code.engineering.redhat.com/gerrit/c/rhs-glusterfs/+/245613
Tested-by: RHGS Build Bot <nigelb@redhat.com>
Reviewed-by: Ravishankar Narayanankutty <ravishankar@redhat.com>
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
---
 tests/bugs/locks/issue-2443-crash.c     | 67 +++++++++++++++++++++++++++++++++
 tests/bugs/locks/issue-2443-crash.t     | 18 +++++++++
 xlators/protocol/client/src/client-lk.c |  1 +
 3 files changed, 86 insertions(+)
 create mode 100644 tests/bugs/locks/issue-2443-crash.c
 create mode 100644 tests/bugs/locks/issue-2443-crash.t

diff --git a/tests/bugs/locks/issue-2443-crash.c b/tests/bugs/locks/issue-2443-crash.c
new file mode 100644
index 0000000..5f580bf
--- /dev/null
+++ b/tests/bugs/locks/issue-2443-crash.c
@@ -0,0 +1,67 @@
+#include <sys/file.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+int
+main(int argc, char *argv[])
+{
+    int fd = -1;
+    char *filename = NULL;
+    struct flock lock = {
+        0,
+    };
+    int i = 0;
+    int ret = -1;
+
+    if (argc != 2) {
+        fprintf(stderr, "Usage: %s <filename> ", argv[0]);
+        goto out;
+    }
+
+    filename = argv[1];
+
+    fd = open(filename, O_RDWR | O_CREAT, 0);
+    if (fd < 0) {
+        fprintf(stderr, "open (%s) failed (%s)\n", filename, strerror(errno));
+        goto out;
+    }
+
+    lock.l_start = 0;
+    lock.l_type = F_RDLCK;
+    lock.l_whence = SEEK_SET;
+    lock.l_len = 2;
+
+    ret = fcntl(fd, F_SETLK, &lock);
+    if (ret < 0) {
+        fprintf(stderr, "fcntl setlk failed (%s)\n", strerror(errno));
+        goto out;
+    }
+
+    lock.l_start = 2;
+    lock.l_type = F_WRLCK;
+    lock.l_whence = SEEK_SET;
+    lock.l_len = 2;
+
+    ret = fcntl(fd, F_SETLK, &lock);
+    if (ret < 0) {
+        fprintf(stderr, "fcntl setlk failed (%s)\n", strerror(errno));
+        goto out;
+    }
+
+    lock.l_start = 0;
+    lock.l_type = F_RDLCK;
+    lock.l_whence = SEEK_SET;
+    lock.l_len = 4;
+
+    ret = fcntl(fd, F_SETLK, &lock);
+    if (ret < 0) {
+        fprintf(stderr, "fcntl setlk failed (%s)\n", strerror(errno));
+        goto out;
+    }
+out:
+    return ret;
+}
diff --git a/tests/bugs/locks/issue-2443-crash.t b/tests/bugs/locks/issue-2443-crash.t
new file mode 100644
index 0000000..162a4d7
--- /dev/null
+++ b/tests/bugs/locks/issue-2443-crash.t
@@ -0,0 +1,18 @@
+#!/bin/bash
+. $(dirname $0)/../../include.rc
+. $(dirname $0)/../../volume.rc
+cleanup;
+
+TEST glusterd
+TEST pidof glusterd
+TEST $CLI volume create $V0 $H0:$B0/brick0
+TEST $CLI volume start $V0
+TEST $GFS --volfile-id=$V0 --volfile-server=$H0 $M0;
+
+build_tester $(dirname $0)/issue-2443-crash.c
+TEST mv $(dirname $0)/issue-2443-crash $M0
+cd $M0
+TEST ./issue-2443-crash a
+
+cd -
+cleanup;
diff --git a/xlators/protocol/client/src/client-lk.c b/xlators/protocol/client/src/client-lk.c
index cb4e894..37c1d35 100644
--- a/xlators/protocol/client/src/client-lk.c
+++ b/xlators/protocol/client/src/client-lk.c
@@ -101,6 +101,7 @@ add_locks(client_posix_lock_t *l1, client_posix_lock_t *l2)
     sum = GF_CALLOC(1, sizeof(*sum), gf_client_mt_clnt_lock_t);
     if (!sum)
         return NULL;
+    INIT_LIST_HEAD(&sum->list);
 
     sum->fl_start = min(l1->fl_start, l2->fl_start);
     sum->fl_end = max(l1->fl_end, l2->fl_end);
-- 
1.8.3.1