mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Fredric Cover <fredric.cover.lkernel@gmail.com>
To: Paulo Alcantara <pc@manguebit.org>, Namjae Jeon <linkinjeon@kernel.org>
Cc: ChenXiaoSong <chenxiaosong@chenxiaosong.com>,
	Tom Talpey <tom@talpey.com>,
	Ronnie Sahlberg <ronniesahlberg@gmail.com>,
	Shyam Prasad N <sprasad@microsoft.com>,
	Bharath SM <bharathsm@microsoft.com>,
	linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org,
	Fredric Cover <fredric.cover.lkernel@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH] smb: client: convert DFS prefix from cache charset on reconnect
Date: Sat, 21 Nov 2026 03:09:51 +0000	[thread overview]
Message-ID: <20261121030951.1470502-1-fredric.cover.lkernel@gmail.com> (raw)

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 <fredric.cover.lkernel@gmail.com>
---
 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


                 reply	other threads:[~2026-11-21  3:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261121030951.1470502-1-fredric.cover.lkernel@gmail.com \
    --to=fredric.cover.lkernel@gmail.com \
    --cc=bharathsm@microsoft.com \
    --cc=chenxiaosong@chenxiaosong.com \
    --cc=linkinjeon@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pc@manguebit.org \
    --cc=ronniesahlberg@gmail.com \
    --cc=sprasad@microsoft.com \
    --cc=stable@vger.kernel.org \
    --cc=tom@talpey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®