]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
datasets: don't allow absolute or paths with directory traversal
authorJason Ish <jason.ish@oisf.net>
Tue, 23 May 2023 21:17:59 +0000 (15:17 -0600)
committerVictor Julien <vjulien@oisf.net>
Wed, 14 Jun 2023 05:10:57 +0000 (07:10 +0200)
For dataset filenames coming from rules, do not allow filenames that
are absolute or contain a directory traversal with "..". This prevents
datasets from escaping the define data-directory which may allow a bad
rule to overwrite any file that Suricata has permission to write to.

Add a new configuration option,
"datasets.rules.allow-absolute-filenames" to allow absolute filenames
in dataset rules. This will be a way to revert back to the pre 6.0.13
behavior where save/state rules could use any filename.

Ticket: #6118

src/detect-dataset.c
src/util-path.c
src/util-path.h
suricata.yaml.in

index 27792c083b91df6dfba6aaabcbda0a390a54b4b4..56bbf2689da7b0ebf195ee0c8742965eec95772f 100644 (file)
@@ -303,8 +303,20 @@ static int SetupSavePath(const DetectEngineCtx *de_ctx,
 {
     SCLogDebug("save %s", save);
 
-    if (PathIsAbsolute(save)) {
-        return 0;
+    int allow_absolute = 0;
+    (void)ConfGetBool("datasets.rules.allow-absolute-filenames", &allow_absolute);
+    if (allow_absolute) {
+        SCLogNotice("Allowing absolute filename for dataset rule: %s", save);
+    } else {
+        if (PathIsAbsolute(save)) {
+            SCLogError("Absolute paths not allowed: %s", save);
+            return -1;
+        }
+
+        if (SCPathContainsTraversal(save)) {
+            SCLogError("Directory traversals not allowed: %s", save);
+            return -1;
+        }
     }
 
     // data dir
index ad78f4f67866790609c62dfea1c974b84e42fd10..8182109c684e9d23026ceb37a005610c51d8a7e9 100644 (file)
@@ -246,3 +246,20 @@ const char *SCBasename(const char *path)
 
     return final + 1;
 }
+
+/**
+ * \brief Check for directory traversal
+ *
+ * \param path The path string to check for traversal
+ *
+ * \retval true if directory traversal is found, otherwise false
+ */
+bool SCPathContainsTraversal(const char *path)
+{
+#ifdef OS_WIN32
+    const char *pattern = "..\\";
+#else
+    const char *pattern = "../";
+#endif
+    return strstr(path, pattern) != NULL;
+}
index 8030b3adb15f8100a1ebe3eabb75d399cd8edd84..6f788a8f2513b7880b7deb91668791d339a30b46 100644 (file)
@@ -41,5 +41,6 @@ bool SCIsRegularDirectory(const struct dirent *const dir_entry);
 bool SCIsRegularFile(const struct dirent *const dir_entry);
 char *SCRealPath(const char *path, char *resolved_path);
 const char *SCBasename(const char *path);
+bool SCPathContainsTraversal(const char *path);
 
 #endif /* __UTIL_PATH_H__ */
index 2b7fd3bef497ccf9ff52dad6f43fd68879ad6ef8..c748f0a564564944605de1a82b8410f69b51db9a 100644 (file)
@@ -1158,6 +1158,12 @@ asn1-max-frames: 256
 #   defaults:
 #     memcap: 100mb
 #     hashsize: 2048
+#
+#  rules:
+#    # Set to true to allow absolute filenames and filenames that use
+#    # ".." components to reference parent directories in rules that specify
+#    # their filenames.
+#    #allow-absolute-filenames: false
 
 ##############################################################################
 ##