]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
fs: move name_contains_dotdot() to header
authorChristian Brauner <brauner@kernel.org>
Thu, 12 Jun 2025 13:25:19 +0000 (15:25 +0200)
committerChristian Brauner <brauner@kernel.org>
Mon, 16 Jun 2025 15:01:22 +0000 (17:01 +0200)
Move the helper from the firmware specific code to a header so we can
reuse it for coredump sockets.

Link: https://lore.kernel.org/20250612-work-coredump-massage-v1-5-315c0c34ba94@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
drivers/base/firmware_loader/main.c
include/linux/fs.h

index 44486b2c71728066dc263e1ecf3438ebbcb1d217..6942c62fa59d122b2e8a3d3240d43472007c2d87 100644 (file)
@@ -822,26 +822,6 @@ static void fw_log_firmware_info(const struct firmware *fw, const char *name,
 {}
 #endif
 
-/*
- * Reject firmware file names with ".." path components.
- * There are drivers that construct firmware file names from device-supplied
- * strings, and we don't want some device to be able to tell us "I would like to
- * be sent my firmware from ../../../etc/shadow, please".
- *
- * Search for ".." surrounded by either '/' or start/end of string.
- *
- * This intentionally only looks at the firmware name, not at the firmware base
- * directory or at symlink contents.
- */
-static bool name_contains_dotdot(const char *name)
-{
-       size_t name_len = strlen(name);
-
-       return strcmp(name, "..") == 0 || strncmp(name, "../", 3) == 0 ||
-              strstr(name, "/../") != NULL ||
-              (name_len >= 3 && strcmp(name+name_len-3, "/..") == 0);
-}
-
 /* called from request_firmware() and request_firmware_work_func() */
 static int
 _request_firmware(const struct firmware **firmware_p, const char *name,
@@ -862,6 +842,17 @@ _request_firmware(const struct firmware **firmware_p, const char *name,
                goto out;
        }
 
+
+       /*
+        * Reject firmware file names with ".." path components.
+        * There are drivers that construct firmware file names from
+        * device-supplied strings, and we don't want some device to be
+        * able to tell us "I would like to be sent my firmware from
+        * ../../../etc/shadow, please".
+        *
+        * This intentionally only looks at the firmware name, not at
+        * the firmware base directory or at symlink contents.
+        */
        if (name_contains_dotdot(name)) {
                dev_warn(device,
                         "Firmware load for '%s' refused, path contains '..' component\n",
index 96c7925a655199ebdfefafa587ea5ae3e3665867..18fdbd184eea8f2139e25f5aa97984b2acc229f5 100644 (file)
@@ -3264,6 +3264,22 @@ static inline bool is_dot_dotdot(const char *name, size_t len)
                (len == 1 || (len == 2 && name[1] == '.'));
 }
 
+/**
+ * name_contains_dotdot - check if a file name contains ".." path components
+ *
+ * Search for ".." surrounded by either '/' or start/end of string.
+ */
+static inline bool name_contains_dotdot(const char *name)
+{
+       size_t name_len;
+
+       name_len = strlen(name);
+       return strcmp(name, "..") == 0 ||
+              strncmp(name, "../", 3) == 0 ||
+              strstr(name, "/../") != NULL ||
+              (name_len >= 3 && strcmp(name + name_len - 3, "/..") == 0);
+}
+
 #include <linux/err.h>
 
 /* needed for stackable file system support */