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