190130
From 56c8ef4a64506c64aeb95d5a2c38d7107f90ac3a Mon Sep 17 00:00:00 2001
190130
From: Xavi Hernandez <xhernandez@redhat.com>
190130
Date: Tue, 5 Feb 2019 16:57:52 +0100
190130
Subject: [PATCH 442/449] fuse: correctly handle setxattr values
190130
190130
The setxattr function receives a pointer to raw data, which may not be
190130
null-terminated. When this data needs to be interpreted as a string, an
190130
explicit null termination needs to be added before using the value.
190130
190130
Upstream patch https://review.gluster.org/#/c/glusterfs/+/22157
190130
> Change-Id: Id110f9b215b22786da5782adec9449ce38d0d563
190130
> updates: bz#1193929
190130
> Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
190130
190130
Note: this change is not addressing the issue of bz 1787310,
190130
indeed it is prerequisite for other changes that do.
190130
190130
BUG: 1787310
190130
Change-Id: I56417b130eb2a1f388108456c905a577eb658793
190130
Signed-off-by: Csaba Henk <csaba@redhat.com>
190130
Reviewed-on: https://code.engineering.redhat.com/gerrit/202758
190130
Tested-by: RHGS Build Bot <nigelb@redhat.com>
190130
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
190130
---
190130
 libglusterfs/src/glusterfs/xlator.h  |  2 +-
190130
 libglusterfs/src/xlator.c            | 28 +++++++++++++++++++++++++---
190130
 xlators/mount/fuse/src/fuse-bridge.c | 20 ++++++++++++++++----
190130
 3 files changed, 42 insertions(+), 8 deletions(-)
190130
190130
diff --git a/libglusterfs/src/glusterfs/xlator.h b/libglusterfs/src/glusterfs/xlator.h
190130
index db04c4d..8650ccc 100644
190130
--- a/libglusterfs/src/glusterfs/xlator.h
190130
+++ b/libglusterfs/src/glusterfs/xlator.h
190130
@@ -1043,7 +1043,7 @@ xlator_mem_acct_init(xlator_t *xl, int num_types);
190130
 void
190130
 xlator_mem_acct_unref(struct mem_acct *mem_acct);
190130
 int
190130
-is_gf_log_command(xlator_t *trans, const char *name, char *value);
190130
+is_gf_log_command(xlator_t *trans, const char *name, char *value, size_t size);
190130
 int
190130
 glusterd_check_log_level(const char *value);
190130
 int
190130
diff --git a/libglusterfs/src/xlator.c b/libglusterfs/src/xlator.c
190130
index 6bd4f09..108b96a 100644
190130
--- a/libglusterfs/src/xlator.c
190130
+++ b/libglusterfs/src/xlator.c
190130
@@ -1278,8 +1278,21 @@ xlator_destroy(xlator_t *xl)
190130
     return 0;
190130
 }
190130
 
190130
+static int32_t
190130
+gf_bin_to_string(char *dst, size_t size, void *src, size_t len)
190130
+{
190130
+    if (len >= size) {
190130
+        return EINVAL;
190130
+    }
190130
+
190130
+    memcpy(dst, src, len);
190130
+    dst[len] = 0;
190130
+
190130
+    return 0;
190130
+}
190130
+
190130
 int
190130
-is_gf_log_command(xlator_t *this, const char *name, char *value)
190130
+is_gf_log_command(xlator_t *this, const char *name, char *value, size_t size)
190130
 {
190130
     xlator_t *trav = NULL;
190130
     char key[1024] = {
190130
@@ -1291,7 +1304,11 @@ is_gf_log_command(xlator_t *this, const char *name, char *value)
190130
     glusterfs_ctx_t *ctx = NULL;
190130
 
190130
     if (!strcmp("trusted.glusterfs.syslog", name)) {
190130
-        ret = gf_string2boolean(value, &syslog_flag);
190130
+        ret = gf_bin_to_string(key, sizeof(key), value, size);
190130
+        if (ret != 0) {
190130
+            goto out;
190130
+        }
190130
+        ret = gf_string2boolean(key, &syslog_flag);
190130
         if (ret) {
190130
             ret = EOPNOTSUPP;
190130
             goto out;
190130
@@ -1307,7 +1324,12 @@ is_gf_log_command(xlator_t *this, const char *name, char *value)
190130
     if (fnmatch("trusted.glusterfs*set-log-level", name, FNM_NOESCAPE))
190130
         goto out;
190130
 
190130
-    log_level = glusterd_check_log_level(value);
190130
+    ret = gf_bin_to_string(key, sizeof(key), value, size);
190130
+    if (ret != 0) {
190130
+        goto out;
190130
+    }
190130
+
190130
+    log_level = glusterd_check_log_level(key);
190130
     if (log_level == -1) {
190130
         ret = EOPNOTSUPP;
190130
         goto out;
190130
diff --git a/xlators/mount/fuse/src/fuse-bridge.c b/xlators/mount/fuse/src/fuse-bridge.c
190130
index 2e7584c..cfad2b4 100644
190130
--- a/xlators/mount/fuse/src/fuse-bridge.c
190130
+++ b/xlators/mount/fuse/src/fuse-bridge.c
190130
@@ -4112,7 +4112,7 @@ fuse_setxattr(xlator_t *this, fuse_in_header_t *finh, void *msg,
190130
 
190130
     /* Check if the command is for changing the log
190130
        level of process or specific xlator */
190130
-    ret = is_gf_log_command(this, name, value);
190130
+    ret = is_gf_log_command(this, name, value, fsi->size);
190130
     if (ret >= 0) {
190130
         op_errno = ret;
190130
         goto done;
190130
@@ -4159,11 +4159,23 @@ fuse_setxattr(xlator_t *this, fuse_in_header_t *finh, void *msg,
190130
          * fixups to make sure that's the case.  To avoid nasty
190130
          * surprises, allocate an extra byte and add a NUL here.
190130
          */
190130
-        dict_value = memdup(value, fsi->size + 1);
190130
+        dict_value = GF_MALLOC(fsi->size + 1, gf_common_mt_char);
190130
+        if (dict_value == NULL) {
190130
+            gf_log("glusterfs-fuse", GF_LOG_ERROR,
190130
+                   "%" PRIu64 ": SETXATTR value allocation failed",
190130
+                   finh->unique);
190130
+            op_errno = ENOMEM;
190130
+            goto done;
190130
+        }
190130
+        memcpy(dict_value, value, fsi->size);
190130
         dict_value[fsi->size] = '\0';
190130
     }
190130
-    dict_set(state->xattr, newkey,
190130
-             data_from_dynptr((void *)dict_value, fsi->size));
190130
+    ret = dict_set_dynptr(state->xattr, newkey, dict_value, fsi->size);
190130
+    if (ret < 0) {
190130
+        op_errno = -ret;
190130
+        GF_FREE(dict_value);
190130
+        goto done;
190130
+    }
190130
 
190130
     state->flags = fsi->flags;
190130
     state->name = newkey;
190130
-- 
190130
1.8.3.1
190130