Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cifs: improve checking of DFS links over STATUS_OBJECT_NAME_INVALID

Do not map STATUS_OBJECT_NAME_INVALID to -EREMOTE under non-DFS
shares, or 'nodfs' mounts or CONFIG_CIFS_DFS_UPCALL=n builds.
Otherwise, in the slow path, get a referral to figure out whether it
is an actual DFS link.

This could be simply reproduced under a non-DFS share by running the
following

$ mount.cifs //srv/share /mnt -o ...
$ cat /mnt/$(printf '\U110000')
cat: '/mnt/'$'\364\220\200\200': Object is remote

Fixes: c877ce47e137 ("cifs: reduce roundtrips on create/qinfo requests")
CC: stable@vger.kernel.org # 6.2
Signed-off-by: Paulo Alcantara (SUSE) <pc@manguebit.com>
Reviewed-by: Ronnie Sahlberg <lsahlber@redhat.com>
Signed-off-by: Steve French <stfrench@microsoft.com>

authored by

Paulo Alcantara and committed by
Steve French
b9ee2e30 4c0421fa

+106 -25
+15 -5
fs/cifs/cifsproto.h
··· 667 667 int match_target_ip(struct TCP_Server_Info *server, 668 668 const char *share, size_t share_len, 669 669 bool *result); 670 - 671 - int cifs_dfs_query_info_nonascii_quirk(const unsigned int xid, 672 - struct cifs_tcon *tcon, 673 - struct cifs_sb_info *cifs_sb, 674 - const char *dfs_link_path); 670 + int cifs_inval_name_dfs_link_error(const unsigned int xid, 671 + struct cifs_tcon *tcon, 672 + struct cifs_sb_info *cifs_sb, 673 + const char *full_path, 674 + bool *islink); 675 + #else 676 + static inline int cifs_inval_name_dfs_link_error(const unsigned int xid, 677 + struct cifs_tcon *tcon, 678 + struct cifs_sb_info *cifs_sb, 679 + const char *full_path, 680 + bool *islink) 681 + { 682 + *islink = false; 683 + return 0; 684 + } 675 685 #endif 676 686 677 687 static inline int cifs_create_options(struct cifs_sb_info *cifs_sb, int options)
+67
fs/cifs/misc.c
··· 21 21 #include "cifsfs.h" 22 22 #ifdef CONFIG_CIFS_DFS_UPCALL 23 23 #include "dns_resolve.h" 24 + #include "dfs_cache.h" 24 25 #endif 25 26 #include "fs_context.h" 26 27 #include "cached_dir.h" ··· 1197 1196 cifs_sb->prepath = NULL; 1198 1197 1199 1198 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; 1199 + return 0; 1200 + } 1201 + 1202 + /* 1203 + * Handle weird Windows SMB server behaviour. It responds with 1204 + * STATUS_OBJECT_NAME_INVALID code to SMB2 QUERY_INFO request for 1205 + * "\<server>\<dfsname>\<linkpath>" DFS reference, where <dfsname> contains 1206 + * non-ASCII unicode symbols. 1207 + */ 1208 + int cifs_inval_name_dfs_link_error(const unsigned int xid, 1209 + struct cifs_tcon *tcon, 1210 + struct cifs_sb_info *cifs_sb, 1211 + const char *full_path, 1212 + bool *islink) 1213 + { 1214 + struct cifs_ses *ses = tcon->ses; 1215 + size_t len; 1216 + char *path; 1217 + char *ref_path; 1218 + 1219 + *islink = false; 1220 + 1221 + /* 1222 + * Fast path - skip check when @full_path doesn't have a prefix path to 1223 + * look up or tcon is not DFS. 1224 + */ 1225 + if (strlen(full_path) < 2 || !cifs_sb || 1226 + (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) || 1227 + !is_tcon_dfs(tcon) || !ses->server->origin_fullpath) 1228 + return 0; 1229 + 1230 + /* 1231 + * Slow path - tcon is DFS and @full_path has prefix path, so attempt 1232 + * to get a referral to figure out whether it is an DFS link. 1233 + */ 1234 + len = strnlen(tcon->tree_name, MAX_TREE_SIZE + 1) + strlen(full_path) + 1; 1235 + path = kmalloc(len, GFP_KERNEL); 1236 + if (!path) 1237 + return -ENOMEM; 1238 + 1239 + scnprintf(path, len, "%s%s", tcon->tree_name, full_path); 1240 + ref_path = dfs_cache_canonical_path(path + 1, cifs_sb->local_nls, 1241 + cifs_remap(cifs_sb)); 1242 + kfree(path); 1243 + 1244 + if (IS_ERR(ref_path)) { 1245 + if (PTR_ERR(ref_path) != -EINVAL) 1246 + return PTR_ERR(ref_path); 1247 + } else { 1248 + struct dfs_info3_param *refs = NULL; 1249 + int num_refs = 0; 1250 + 1251 + /* 1252 + * XXX: we are not using dfs_cache_find() here because we might 1253 + * end filling all the DFS cache and thus potentially 1254 + * removing cached DFS targets that the client would eventually 1255 + * need during failover. 1256 + */ 1257 + if (ses->server->ops->get_dfs_refer && 1258 + !ses->server->ops->get_dfs_refer(xid, ses, ref_path, &refs, 1259 + &num_refs, cifs_sb->local_nls, 1260 + cifs_remap(cifs_sb))) 1261 + *islink = refs[0].server_type == DFS_TYPE_LINK; 1262 + free_dfs_info_array(refs, num_refs); 1263 + kfree(ref_path); 1264 + } 1200 1265 return 0; 1201 1266 } 1202 1267 #endif
+11 -10
fs/cifs/smb2inode.c
··· 527 527 struct cifs_sb_info *cifs_sb, const char *full_path, 528 528 struct cifs_open_info_data *data, bool *adjust_tz, bool *reparse) 529 529 { 530 - int rc; 531 530 __u32 create_options = 0; 532 531 struct cifsFileInfo *cfile; 533 532 struct cached_fid *cfid = NULL; 534 533 struct kvec err_iov[3] = {}; 535 534 int err_buftype[3] = {}; 535 + bool islink; 536 + int rc, rc2; 536 537 537 538 *adjust_tz = false; 538 539 *reparse = false; ··· 581 580 SMB2_OP_QUERY_INFO, cfile, NULL, NULL, 582 581 NULL, NULL); 583 582 goto out; 584 - } else if (rc != -EREMOTE && IS_ENABLED(CONFIG_CIFS_DFS_UPCALL) && 585 - hdr->Status == STATUS_OBJECT_NAME_INVALID) { 586 - /* 587 - * Handle weird Windows SMB server behaviour. It responds with 588 - * STATUS_OBJECT_NAME_INVALID code to SMB2 QUERY_INFO request 589 - * for "\<server>\<dfsname>\<linkpath>" DFS reference, 590 - * where <dfsname> contains non-ASCII unicode symbols. 591 - */ 592 - rc = -EREMOTE; 583 + } else if (rc != -EREMOTE && hdr->Status == STATUS_OBJECT_NAME_INVALID) { 584 + rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb, 585 + full_path, &islink); 586 + if (rc2) { 587 + rc = rc2; 588 + goto out; 589 + } 590 + if (islink) 591 + rc = -EREMOTE; 593 592 } 594 593 if (rc == -EREMOTE && IS_ENABLED(CONFIG_CIFS_DFS_UPCALL) && cifs_sb && 595 594 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS))
+13 -10
fs/cifs/smb2ops.c
··· 796 796 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon, 797 797 struct cifs_sb_info *cifs_sb, const char *full_path) 798 798 { 799 - int rc; 800 799 __le16 *utf16_path; 801 800 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE; 802 801 int err_buftype = CIFS_NO_BUFFER; ··· 803 804 struct kvec err_iov = {}; 804 805 struct cifs_fid fid; 805 806 struct cached_fid *cfid; 807 + bool islink; 808 + int rc, rc2; 806 809 807 810 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, true, &cfid); 808 811 if (!rc) { ··· 834 833 835 834 if (unlikely(!hdr || err_buftype == CIFS_NO_BUFFER)) 836 835 goto out; 837 - /* 838 - * Handle weird Windows SMB server behaviour. It responds with 839 - * STATUS_OBJECT_NAME_INVALID code to SMB2 QUERY_INFO request 840 - * for "\<server>\<dfsname>\<linkpath>" DFS reference, 841 - * where <dfsname> contains non-ASCII unicode symbols. 842 - */ 843 - if (rc != -EREMOTE && IS_ENABLED(CONFIG_CIFS_DFS_UPCALL) && 844 - hdr->Status == STATUS_OBJECT_NAME_INVALID) 845 - rc = -EREMOTE; 836 + 837 + if (rc != -EREMOTE && hdr->Status == STATUS_OBJECT_NAME_INVALID) { 838 + rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb, 839 + full_path, &islink); 840 + if (rc2) { 841 + rc = rc2; 842 + goto out; 843 + } 844 + if (islink) 845 + rc = -EREMOTE; 846 + } 846 847 if (rc == -EREMOTE && IS_ENABLED(CONFIG_CIFS_DFS_UPCALL) && cifs_sb && 847 848 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)) 848 849 rc = -EOPNOTSUPP;