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