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