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