From 528a19825420362b4b873bbd4070b0496ab86a39 Mon Sep 17 00:00:00 2001 From: Aravinda VK Date: Wed, 22 Jun 2016 14:57:44 +0530 Subject: glusterd/geo-rep: Add relative path validation to copy file command Added validation for input file, command fails if input file path is relative path pointing outside of GLUSTERD_WORKDIR. BUG: 1350785 Change-Id: I329d43ebed69bfe9fe03d6be70dc8c78a605ffc5 Signed-off-by: Aravinda VK Reviewed-on: http://review.gluster.org/14772 (cherry picked from commit 888de8851e718d8e3117e47fa35cfc075b998f62) Reviewed-on: http://review.gluster.org/14819 Smoke: Gluster Build System NetBSD-regression: NetBSD Build System CentOS-regression: Gluster Build System Reviewed-by: Jeff Darcy --- xlators/mgmt/glusterd/src/glusterd-geo-rep.c | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/xlators/mgmt/glusterd/src/glusterd-geo-rep.c b/xlators/mgmt/glusterd/src/glusterd-geo-rep.c index 2503bded1fe..625c355c7b0 100644 --- a/xlators/mgmt/glusterd/src/glusterd-geo-rep.c +++ b/xlators/mgmt/glusterd/src/glusterd-geo-rep.c @@ -2397,6 +2397,9 @@ glusterd_op_stage_copy_file (dict_t *dict, char **op_errstr) glusterd_conf_t *priv = NULL; struct stat stbuf = {0,}; xlator_t *this = NULL; + char workdir[PATH_MAX] = {0,}; + char realpath_filename[PATH_MAX] = {0,}; + char realpath_workdir[PATH_MAX] = {0,}; this = THIS; GF_ASSERT (this); @@ -2441,6 +2444,37 @@ glusterd_op_stage_copy_file (dict_t *dict, char **op_errstr) snprintf (abs_filename, sizeof(abs_filename), "%s/%s", priv->workdir, filename); + if (!realpath (priv->workdir, realpath_workdir)) { + snprintf (errmsg, sizeof (errmsg), "Failed to get " + "realpath of %s: %s", priv->workdir, + strerror (errno)); + *op_errstr = gf_strdup (errmsg); + ret = -1; + goto out; + } + + if (!realpath (abs_filename, realpath_filename)) { + snprintf (errmsg, sizeof (errmsg), "Failed to get " + "realpath of %s: %s", filename, + strerror (errno)); + *op_errstr = gf_strdup (errmsg); + ret = -1; + goto out; + } + + /* Add Trailing slash to workdir, without slash strncmp + will succeed for /var/lib/glusterd_bad */ + snprintf (workdir, sizeof(workdir), "%s/", realpath_workdir); + + /* Protect against file copy outside $workdir */ + if (strncmp (workdir, realpath_filename, strlen (workdir))) { + snprintf (errmsg, sizeof (errmsg), "Source file" + " is outside of %s directory", priv->workdir); + *op_errstr = gf_strdup (errmsg); + ret = -1; + goto out; + } + ret = sys_lstat (abs_filename, &stbuf); if (ret) { snprintf (errmsg, sizeof (errmsg), "Source file" -- cgit