a3470f
From 37132b4ea02a57056a426e04e77defb28e447ff9 Mon Sep 17 00:00:00 2001
a3470f
From: Amar Tumballi <amarts@redhat.com>
a3470f
Date: Tue, 24 Jul 2018 15:42:28 +0530
a3470f
Subject: [PATCH 345/351] io-stats: allow only relative path for dumping
a3470f
 io-stats info
a3470f
a3470f
It wouldn't make sense to allow iostats file to be written in
a3470f
*any* directory. While the formating makes sure we try to append
a3470f
io-stats-name for the file, so overwriting existing file is slim,
a3470f
it makes sense to restrict dumping to one directory.
a3470f
a3470f
BUG: 1605086
a3470f
Change-Id: Ie28b6a355de574d8d3855c8654d6756ef0389055
a3470f
Signed-off-by: Amar Tumballi <amarts@redhat.com>
a3470f
Reviewed-on: https://code.engineering.redhat.com/gerrit/145898
a3470f
Reviewed-by: Pranith Kumar Karampuri <pkarampu@redhat.com>
a3470f
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
a3470f
---
a3470f
 tests/bugs/core/io-stats-1322825.t    | 12 ++++++------
a3470f
 xlators/debug/io-stats/src/io-stats.c | 29 ++++++++++++++++++++++-------
a3470f
 2 files changed, 28 insertions(+), 13 deletions(-)
a3470f
a3470f
diff --git a/tests/bugs/core/io-stats-1322825.t b/tests/bugs/core/io-stats-1322825.t
a3470f
index d232ecb..53f2d04 100755
a3470f
--- a/tests/bugs/core/io-stats-1322825.t
a3470f
+++ b/tests/bugs/core/io-stats-1322825.t
a3470f
@@ -23,7 +23,7 @@ TEST $CLI volume profile $V0 start
a3470f
 TEST mkdir $M0/dir1
a3470f
 
a3470f
 # Generate the stat dump across the io-stat instances
a3470f
-TEST setfattr -n trusted.io-stats-dump -v /tmp/io-stats-1322825 $M0
a3470f
+TEST setfattr -n trusted.io-stats-dump -v io-stats-1322825 $M0
a3470f
 
a3470f
 # Check if $M0 is clean w.r.t xattr information
a3470f
 # TODO: if there are better ways to check we really get no attr error, please
a3470f
@@ -42,12 +42,12 @@ ret=$(echo $?)
a3470f
 EXPECT 0 echo $ret
a3470f
 
a3470f
 # Check if we have 5 io-stat files in /tmp
a3470f
-EXPECT 5 ls -1 /tmp/io-stats-1322825*
a3470f
+EXPECT 5 ls -1 /var/run/gluster/io-stats-1322825*
a3470f
 # Cleanup the 5 generated files
a3470f
-rm -f /tmp/io-stats-1322825*
a3470f
+rm -f /var/run/gluster/io-stats-1322825*
a3470f
 
a3470f
 # Rinse and repeat above for a directory
a3470f
-TEST setfattr -n trusted.io-stats-dump -v /tmp/io-stats-1322825 $M0/dir1
a3470f
+TEST setfattr -n trusted.io-stats-dump -v io-stats-1322825 $M0/dir1
a3470f
 getfattr -n trusted.io-stats-dump $B0/${V0}1/dir1 2>&1 | grep -qi "no such attribute"
a3470f
 ret=$(echo $?)
a3470f
 EXPECT 0 echo $ret
a3470f
@@ -61,7 +61,7 @@ getfattr -n trusted.io-stats-dump $B0/${V0}4/dir1 2>&1 | grep -qi "no such attri
a3470f
 ret=$(echo $?)
a3470f
 EXPECT 0 echo $ret
a3470f
 
a3470f
-EXPECT 5 ls -1 /tmp/io-stats-1322825*
a3470f
-rm -f /tmp/io-stats-1322825*
a3470f
+EXPECT 5 ls -1 /var/run/gluster/io-stats-1322825*
a3470f
+rm -f /var/run/gluster/io-stats-1322825*
a3470f
 
a3470f
 cleanup;
a3470f
diff --git a/xlators/debug/io-stats/src/io-stats.c b/xlators/debug/io-stats/src/io-stats.c
a3470f
index f46474b..868890f 100644
a3470f
--- a/xlators/debug/io-stats/src/io-stats.c
a3470f
+++ b/xlators/debug/io-stats/src/io-stats.c
a3470f
@@ -45,6 +45,8 @@
a3470f
 #define DEFAULT_GRP_BUF_SZ 16384
a3470f
 #define IOS_BLOCK_COUNT_SIZE 32
a3470f
 
a3470f
+#define IOS_STATS_DUMP_DIR DEFAULT_VAR_RUN_DIRECTORY
a3470f
+
a3470f
 typedef enum {
a3470f
         IOS_STATS_TYPE_NONE,
a3470f
         IOS_STATS_TYPE_OPEN,
a3470f
@@ -3000,7 +3002,6 @@ io_stats_fsync (call_frame_t *frame, xlator_t *this,
a3470f
         return 0;
a3470f
 }
a3470f
 
a3470f
-
a3470f
 int
a3470f
 conditional_dump (dict_t *dict, char *key, data_t *value, void *data)
a3470f
 {
a3470f
@@ -3016,6 +3017,7 @@ conditional_dump (dict_t *dict, char *key, data_t *value, void *data)
a3470f
         int                   pid, namelen;
a3470f
         char                  dump_key[100];
a3470f
         char                 *slash_ptr = NULL;
a3470f
+        char                 *path_in_value = NULL;
a3470f
 
a3470f
         stub  = data;
a3470f
         this  = stub->this;
a3470f
@@ -3024,13 +3026,26 @@ conditional_dump (dict_t *dict, char *key, data_t *value, void *data)
a3470f
         name as well. This helps when there is more than a single io-stats
a3470f
         instance in the graph, or the client and server processes are running
a3470f
         on the same node */
a3470f
-        /* hmmm... no check for this */
a3470f
-        /* name format: <passed in path/filename>.<xlator name slashes to -> */
a3470f
-        namelen = value->len + strlen (this->name) + 2; /* '.' and '\0' */
a3470f
+        /* For the sanity of where the file should be located, we should make
a3470f
+           sure file is written only inside RUNDIR (ie, /var/run/gluster) */
a3470f
+        /* TODO: provide an option to dump it to different directory of
a3470f
+           choice, based on options */
a3470f
+        /* name format: /var/run/gluster/<passed in path/filename>.<xlator name slashes to -> */
a3470f
+
a3470f
+        path_in_value = data_to_str (value);
a3470f
+
a3470f
+        if (strstr (path_in_value, "../")) {
a3470f
+                gf_log (this->name, GF_LOG_ERROR,
a3470f
+                        "%s: no \"../\" allowed in path", path_in_value);
a3470f
+                return -1;
a3470f
+        }
a3470f
+        namelen = (strlen (IOS_STATS_DUMP_DIR) + value->len +
a3470f
+                   strlen (this->name) + 2);         /* '.' and '\0' */
a3470f
+
a3470f
         filename = alloca0 (namelen);
a3470f
-        memcpy (filename, data_to_str (value), value->len);
a3470f
-        memcpy (filename + value->len, ".", 1);
a3470f
-        memcpy (filename + value->len + 1, this->name, strlen(this->name));
a3470f
+
a3470f
+        snprintf (filename, namelen, "%s%s.%s", IOS_STATS_DUMP_DIR,
a3470f
+                  path_in_value, this->name);
a3470f
 
a3470f
         /* convert any slashes to '-' so that fopen works correctly */
a3470f
         slash_ptr = strchr (filename + value->len + 1, '/');
a3470f
-- 
a3470f
1.8.3.1
a3470f