Blame SOURCES/0129-lib-add-a-function-checking-file-names.patch

5f7b57
From 54ecf8d017580b495d6501e53ca54e453a73a364 Mon Sep 17 00:00:00 2001
5f7b57
From: Jakub Filak <jfilak@redhat.com>
5f7b57
Date: Thu, 23 Apr 2015 13:21:41 +0200
5f7b57
Subject: [LIBREPORT PATCH] lib: add a function checking file names
5f7b57
5f7b57
Move the code from ABRT and extend it a bit:
5f7b57
* allow only 64 characters
5f7b57
* allow '.' in names (vmcore_dmesg.txt)
5f7b57
* forbid '/'
5f7b57
* forbid "."
5f7b57
* forbid ".."
5f7b57
5f7b57
Related: #1214451
5f7b57
5f7b57
Signed-off-by: Jakub Filak <jfilak@redhat.com>
5f7b57
---
5f7b57
 src/include/internal_libreport.h |  6 +++++
5f7b57
 src/lib/concat_path_file.c       | 25 ++++++++++++++++++++
5f7b57
 tests/Makefile.am                |  3 ++-
5f7b57
 tests/dump_dir.at                | 49 ++++++++++++++++++++++++++++++++++++++++
5f7b57
 tests/testsuite.at               |  1 +
5f7b57
 5 files changed, 83 insertions(+), 1 deletion(-)
5f7b57
 create mode 100644 tests/dump_dir.at
5f7b57
5f7b57
diff --git a/src/include/internal_libreport.h b/src/include/internal_libreport.h
5f7b57
index 4c5c72a..8d84fd4 100644
5f7b57
--- a/src/include/internal_libreport.h
5f7b57
+++ b/src/include/internal_libreport.h
5f7b57
@@ -132,6 +132,12 @@ char *concat_path_file(const char *path, const char *filename);
5f7b57
 #define concat_path_basename libreport_concat_path_basename
5f7b57
 char *concat_path_basename(const char *path, const char *filename);
5f7b57
 
5f7b57
+/* Allows all printable characters except '/',
5f7b57
+ * the string must not exceed 64 characters of length
5f7b57
+ * and must not equal neither "." nor ".." (these strings may appear in the string) */
5f7b57
+#define str_is_correct_filename libreport_str_is_correct_filename
5f7b57
+bool str_is_correct_filename(const char *str);
5f7b57
+
5f7b57
 /* A-la fgets, but malloced and of unlimited size */
5f7b57
 #define xmalloc_fgets libreport_xmalloc_fgets
5f7b57
 char *xmalloc_fgets(FILE *file);
5f7b57
diff --git a/src/lib/concat_path_file.c b/src/lib/concat_path_file.c
5f7b57
index 39ae07a..24e4cbd 100644
5f7b57
--- a/src/lib/concat_path_file.c
5f7b57
+++ b/src/lib/concat_path_file.c
5f7b57
@@ -57,3 +57,28 @@ char *concat_path_basename(const char *path, const char *filename)
5f7b57
     free(abspath);
5f7b57
     return name;
5f7b57
 }
5f7b57
+
5f7b57
+bool str_is_correct_filename(const char *str)
5f7b57
+{
5f7b57
+#define NOT_PRINTABLE(c) (c < ' ' || c == 0x7f)
5f7b57
+
5f7b57
+    if (NOT_PRINTABLE(*str) || *str == '/' || *str == '\0')
5f7b57
+        return false;
5f7b57
+    ++str;
5f7b57
+
5f7b57
+    if (NOT_PRINTABLE(*str) || *str =='/' || (*str == '\0' && *(str-1) == '.'))
5f7b57
+        return false;
5f7b57
+    ++str;
5f7b57
+
5f7b57
+    if (NOT_PRINTABLE(*str) || *str =='/' || (*str == '\0' && *(str-1) == '.' && *(str-2) == '.'))
5f7b57
+        return false;
5f7b57
+    ++str;
5f7b57
+
5f7b57
+    for (unsigned i = 0; *str != '\0' && i < 61; ++str, ++i)
5f7b57
+        if (NOT_PRINTABLE(*str) || *str == '/')
5f7b57
+            return false;
5f7b57
+
5f7b57
+    return *str == '\0';
5f7b57
+
5f7b57
+#undef NOT_PRINTABLE
5f7b57
+}
5f7b57
diff --git a/tests/Makefile.am b/tests/Makefile.am
5f7b57
index a680f05..eaf1ac2 100644
5f7b57
--- a/tests/Makefile.am
5f7b57
+++ b/tests/Makefile.am
5f7b57
@@ -42,7 +42,8 @@ TESTSUITE_AT = \
5f7b57
   report_python.at \
5f7b57
   xfuncs.at \
5f7b57
   string_list.at \
5f7b57
-  ureport.at
5f7b57
+  ureport.at \
5f7b57
+  dump_dir.at
5f7b57
 
5f7b57
 EXTRA_DIST += $(TESTSUITE_AT)
5f7b57
 TESTSUITE = $(srcdir)/testsuite
5f7b57
diff --git a/tests/dump_dir.at b/tests/dump_dir.at
5f7b57
new file mode 100644
5f7b57
index 0000000..a579243
5f7b57
--- /dev/null
5f7b57
+++ b/tests/dump_dir.at
5f7b57
@@ -0,0 +1,49 @@
5f7b57
+# -*- Autotest -*-
5f7b57
+
5f7b57
+AT_BANNER([dump directories])
5f7b57
+
5f7b57
+## ----------------------- ##
5f7b57
+## str_is_correct_filename ##
5f7b57
+## ----------------------- ##
5f7b57
+
5f7b57
+AT_TESTFUN([str_is_correct_filename],
5f7b57
+[[
5f7b57
+#include "internal_libreport.h"
5f7b57
+#include <assert.h>
5f7b57
+#
5f7b57
+int main(void)
5f7b57
+{
5f7b57
+    g_verbose = 3;
5f7b57
+
5f7b57
+    assert(str_is_correct_filename("") == false);
5f7b57
+    assert(str_is_correct_filename("/") == false);
5f7b57
+    assert(str_is_correct_filename("//") == false);
5f7b57
+    assert(str_is_correct_filename(".") == false);
5f7b57
+    assert(str_is_correct_filename(".") == false);
5f7b57
+    assert(str_is_correct_filename("..") == false);
5f7b57
+    assert(str_is_correct_filename("..") == false);
5f7b57
+    assert(str_is_correct_filename("/.") == false);
5f7b57
+    assert(str_is_correct_filename("//.") == false);
5f7b57
+    assert(str_is_correct_filename("./") == false);
5f7b57
+    assert(str_is_correct_filename(".//") == false);
5f7b57
+    assert(str_is_correct_filename("/./") == false);
5f7b57
+    assert(str_is_correct_filename("/..") == false);
5f7b57
+    assert(str_is_correct_filename("//..") == false);
5f7b57
+    assert(str_is_correct_filename("../") == false);
5f7b57
+    assert(str_is_correct_filename("..//") == false);
5f7b57
+    assert(str_is_correct_filename("/../") == false);
5f7b57
+    assert(str_is_correct_filename("/.././") == false);
5f7b57
+
5f7b57
+    assert(str_is_correct_filename("looks-good-but-evil/") == false);
5f7b57
+    assert(str_is_correct_filename("looks-good-but-evil/../../") == false);
5f7b57
+
5f7b57
+    assert(str_is_correct_filename(".meta-data") == true);
5f7b57
+    assert(str_is_correct_filename("..meta-meta-data") == true);
5f7b57
+    assert(str_is_correct_filename("meta-..-data") == true);
5f7b57
+
5f7b57
+    assert(str_is_correct_filename("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-") == true);
5f7b57
+    assert(str_is_correct_filename("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-=") == false);
5f7b57
+
5f7b57
+    return 0;
5f7b57
+}
5f7b57
+]])
5f7b57
diff --git a/tests/testsuite.at b/tests/testsuite.at
5f7b57
index abad32b..41107e7 100644
5f7b57
--- a/tests/testsuite.at
5f7b57
+++ b/tests/testsuite.at
5f7b57
@@ -17,3 +17,4 @@ m4_include([xml_definition.at])
5f7b57
 m4_include([report_python.at])
5f7b57
 m4_include([string_list.at])
5f7b57
 m4_include([ureport.at])
5f7b57
+m4_include([dump_dir.at])
5f7b57
-- 
5f7b57
1.8.3.1
5f7b57