CVE-2022-3592 lib: Move subdir_of() to source3/lib/util_path.c
authorVolker Lendecke <vl@samba.org>
Sat, 15 Oct 2022 11:29:14 +0000 (13:29 +0200)
committerJule Anger <janger@samba.org>
Tue, 25 Oct 2022 10:31:34 +0000 (10:31 +0000)
Make it available for other components

Bug: https://bugzilla.samba.org/show_bug.cgi?id=15207
Signed-off-by: Volker Lendecke <vl@samba.org>
source3/lib/util_path.c
source3/lib/util_path.h
source3/smbd/open.c

index 33827fcf89450e84d457d7ac3c316895fe155bbc..5a94b391dd642e4c1e57f4e1fb1eb7d7a3acc6fa 100644 (file)
@@ -304,3 +304,53 @@ bool extract_snapshot_token(char *fname, NTTIME *twrp)
 
        return true;
 }
+
+/*
+ * Take two absolute paths, figure out if "subdir" is a proper
+ * subdirectory of "parent". Return the component relative to the
+ * "parent" without the potential "/". Take care of "parent"
+ * possibly ending in "/".
+ */
+bool subdir_of(const char *parent,
+              size_t parent_len,
+              const char *subdir,
+              const char **_relative)
+{
+       const char *relative = NULL;
+       bool matched;
+
+       SMB_ASSERT(parent[0] == '/');
+       SMB_ASSERT(subdir[0] == '/');
+
+       if (parent_len == 1) {
+               /*
+                * Everything is below "/"
+                */
+               *_relative = subdir+1;
+               return true;
+       }
+
+       if (parent[parent_len-1] == '/') {
+               parent_len -= 1;
+       }
+
+       matched = (strncmp(subdir, parent, parent_len) == 0);
+       if (!matched) {
+               return false;
+       }
+
+       relative = &subdir[parent_len];
+
+       if (relative[0] == '\0') {
+               *_relative = relative; /* nothing left */
+               return true;
+       }
+
+       if (relative[0] == '/') {
+               /* End of parent must match a '/' in subdir. */
+               *_relative = relative+1;
+               return true;
+       }
+
+       return false;
+}
index 33699da28c243a9ee0bb133f34def3f3c1fb411f..5b0a1f13e380ffb2690e3bec67d92c52701b7674 100644 (file)
@@ -49,5 +49,9 @@ bool clistr_is_previous_version_path(const char *path,
                                     const char **startp,
                                     const char **endp,
                                     NTTIME *ptwrp);
+bool subdir_of(const char *parent,
+              size_t parent_len,
+              const char *subdir,
+              const char **_relative);
 
 #endif
index 9d85b0d8ef866bc666cd273a969910555eaccc3a..d4a679a4cb0fc4042ae7fa786a78f8cd60447095 100644 (file)
@@ -475,58 +475,6 @@ static NTSTATUS check_base_file_access(struct files_struct *fsp,
                                        access_mask);
 }
 
-/*
- * Take two absolute paths, figure out if "subdir" is a proper
- * subdirectory of "parent". Return the component relative to the
- * "parent" without the potential "/". Take care of "parent"
- * possibly ending in "/".
- */
-static bool subdir_of(
-       const char *parent,
-       size_t parent_len,
-       const char *subdir,
-       const char **_relative)
-
-{
-       const char *relative = NULL;
-       bool matched;
-
-       SMB_ASSERT(parent[0] == '/');
-       SMB_ASSERT(subdir[0] == '/');
-
-       if (parent_len == 1) {
-               /*
-                * Everything is below "/"
-                */
-               *_relative = subdir+1;
-               return true;
-       }
-
-       if (parent[parent_len-1] == '/') {
-               parent_len -= 1;
-       }
-
-       matched = (strncmp(subdir, parent, parent_len) == 0);
-       if (!matched) {
-               return false;
-       }
-
-       relative = &subdir[parent_len];
-
-       if (relative[0] == '\0') {
-               *_relative = relative; /* nothing left */
-               return true;
-       }
-
-       if (relative[0] == '/') {
-               /* End of parent must match a '/' in subdir. */
-               *_relative = relative+1;
-               return true;
-       }
-
-       return false;
-}
-
 static NTSTATUS chdir_below_conn(
        TALLOC_CTX *mem_ctx,
        connection_struct *conn,