The DFS referral cache stores paths and target strings in UTF-8 (cache_cp), whereas cifs_sb->prepath must be encoded in the mount's local charset (cifs_sb->local_nls). When reconnecting a DFS link in tree_connect_dfs_target(), dfs_cache_get_tgt_share() returns @prefix in cache_cp, which was passed directly to cifs_update_super_prepath(). On mounts using a non-UTF-8 iocharset such as iso8859-1, this overwrites cifs_sb->prepath with UTF-8 bytes. Subsequent operations then re-encode cifs_sb->prepath to UTF-16LE using cifs_sb->local_nls, corrupting non-ASCII characters on the wire and causing lookups to fail with -ENOENT. Add dfs_cache_local_path() to convert a path from cache_cp to a caller-specified codepage via UTF-16, and use it in a new set_super_prepath_from_cache() helper called by tree_connect_dfs_target() so cifs_sb->prepath stays in cifs_sb->local_nls across reconnects. Tested against Samba with iocharset=utf8 and iocharset=iso8859-1 across multiple smbd restarts. Fixes: c870a8e70e68 ("cifs: handle different charsets in dfs cache") Cc: stable@vger.kernel.org Signed-off-by: Fredric Cover --- fs/smb/client/dfs.c | 18 +++++++++++++++++- fs/smb/client/dfs_cache.c | 31 +++++++++++++++++++++++++++++++ fs/smb/client/dfs_cache.h | 1 + 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/fs/smb/client/dfs.c b/fs/smb/client/dfs.c index 788cdda9a573..efef00c33321 100644 --- a/fs/smb/client/dfs.c +++ b/fs/smb/client/dfs.c @@ -404,6 +404,22 @@ static int target_share_matches_server(struct TCP_Server_Info *server, char *sha return rc; } +/* @prefix is in the DFS cache charset, the superblock prefix in the mount charset */ +static int set_super_prepath_from_cache(struct cifs_sb_info *cifs_sb, const char *prefix) +{ + char *lprefix = NULL; + int rc; + + if (prefix && *prefix) { + lprefix = dfs_cache_local_path(prefix, cifs_sb->local_nls); + if (IS_ERR(lprefix)) + return PTR_ERR(lprefix); + } + rc = cifs_update_super_prepath(cifs_sb, lprefix); + kfree(lprefix); + return rc; +} + static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon, struct cifs_sb_info *cifs_sb, @@ -444,7 +460,7 @@ static int tree_connect_dfs_target(const unsigned int xid, rc = ops->tree_connect(xid, tcon->ses, tree, tcon); if (islink && !rc && cifs_sb) - rc = cifs_update_super_prepath(cifs_sb, prefix); + rc = set_super_prepath_from_cache(cifs_sb, prefix); break; } diff --git a/fs/smb/client/dfs_cache.c b/fs/smb/client/dfs_cache.c index 02864f7acefc..168f563d5ef6 100644 --- a/fs/smb/client/dfs_cache.c +++ b/fs/smb/client/dfs_cache.c @@ -105,6 +105,37 @@ char *dfs_cache_canonical_path(const char *path, const struct nls_table *cp, int return npath; } +/** + * dfs_cache_local_path - convert a path from the DFS cache charset + * + * @path: path in the DFS cache charset + * @cp: codepage to convert to + * + * Return the converted path, which the caller must free, or an error pointer. + */ +char *dfs_cache_local_path(const char *path, const struct nls_table *cp) +{ + char *tmp, *npath; + int plen = 0; + + if (!path || !cp || unlikely(!cache_cp)) + return ERR_PTR(-EINVAL); + + if (!strcmp(cp->charset, cache_cp->charset)) { + npath = kstrdup(path, GFP_KERNEL); + return npath ?: ERR_PTR(-ENOMEM); + } + + tmp = (char *)cifs_strndup_to_utf16(path, strlen(path), &plen, + cache_cp, NO_MAP_UNI_RSVD); + if (!tmp) + return ERR_PTR(-ENOMEM); + + npath = cifs_strndup_from_utf16(tmp, plen, true, cp); + kfree(tmp); + return npath ?: ERR_PTR(-ENOMEM); +} + static inline bool cache_entry_expired(const struct cache_entry *ce) { struct timespec64 ts; diff --git a/fs/smb/client/dfs_cache.h b/fs/smb/client/dfs_cache.h index c99dc3546c70..636eb1554311 100644 --- a/fs/smb/client/dfs_cache.h +++ b/fs/smb/client/dfs_cache.h @@ -53,6 +53,7 @@ int dfs_cache_get_tgt_share(char *path, char **share, char **prefix); char *dfs_cache_canonical_path(const char *path, const struct nls_table *cp, int remap); +char *dfs_cache_local_path(const char *path, const struct nls_table *cp); int dfs_cache_remount_fs(struct cifs_sb_info *cifs_sb); void dfs_cache_refresh(struct work_struct *work); -- 2.53.0