]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
smb: client: validate DFS referral PathConsumed
authorYichong Chen <chenyichong@uniontech.com>
Thu, 16 Jul 2026 05:25:23 +0000 (13:25 +0800)
committerSteve French <stfrench@microsoft.com>
Mon, 20 Jul 2026 03:33:38 +0000 (22:33 -0500)
parse_dfs_referrals() validates that the response contains the fixed
referral entry array and, on for-next, the per-referral string offsets.
However, the response also contains a PathConsumed value that is later
used for DFS path parsing.

If a malformed response provides a PathConsumed value larger than the
search name, later DFS parsing can advance beyond the end of the path.

Validate PathConsumed against the search name length before storing it in
the parsed referral.

Fixes: 4ecce920e13a ("CIFS: move DFS response parsing out of SMB1 code")
Reviewed-by: Paulo Alcantara (Red Hat) <pc@manguebit.org>
Signed-off-by: Yichong Chen <chenyichong@uniontech.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
fs/smb/client/misc.c

index e4bac2a0b85dcfead7812fa811b53f8ff3871b19..b9c59b2cf76ab05d3e46a3c8d574423a2936e951 100644 (file)
@@ -682,6 +682,8 @@ parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size,
        int i, rc = 0;
        char *data_end;
        struct dfs_referral_level_3 *ref;
+       unsigned int path_consumed;
+       size_t search_name_len;
 
        if (rsp_size < sizeof(*rsp)) {
                cifs_dbg(VFS | ONCE,
@@ -728,6 +730,7 @@ parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size,
                rc = -ENOMEM;
                goto parse_DFS_referrals_exit;
        }
+       search_name_len = strlen(searchName);
 
        /* collect necessary data from referrals */
        for (i = 0; i < *num_of_nodes; i++) {
@@ -736,21 +739,34 @@ parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size,
                struct dfs_info3_param *node = (*target_nodes)+i;
 
                node->flags = le32_to_cpu(rsp->DFSFlags);
+               path_consumed = le16_to_cpu(rsp->PathConsumed);
                if (is_unicode) {
-                       __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
-                                               GFP_KERNEL);
-                       if (tmp == NULL) {
+                       size_t search_name_utf16_len = search_name_len * 2 + 2;
+                       __le16 *tmp;
+
+                       if (path_consumed > search_name_utf16_len) {
+                               rc = -EINVAL;
+                               goto parse_DFS_referrals_exit;
+                       }
+
+                       tmp = kmalloc(search_name_utf16_len, GFP_KERNEL);
+                       if (!tmp) {
                                rc = -ENOMEM;
                                goto parse_DFS_referrals_exit;
                        }
-                       cifsConvertToUTF16((__le16 *) tmp, searchName,
+                       cifsConvertToUTF16((__le16 *)tmp, searchName,
                                           PATH_MAX, nls_codepage, remap);
-                       node->path_consumed = cifs_utf16_bytes(tmp,
-                                       le16_to_cpu(rsp->PathConsumed),
-                                       nls_codepage);
+                       node->path_consumed = cifs_utf16_bytes(tmp, path_consumed,
+                                                              nls_codepage);
                        kfree(tmp);
-               } else
-                       node->path_consumed = le16_to_cpu(rsp->PathConsumed);
+               } else {
+                       if (path_consumed > search_name_len) {
+                               rc = -EINVAL;
+                               goto parse_DFS_referrals_exit;
+                       }
+
+                       node->path_consumed = path_consumed;
+               }
 
                node->server_type = le16_to_cpu(ref->ServerType);
                node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);