cifs: Add new mount option -o nounicode to disable SMB1 UNICODE mode
authorPali Rohár <pali@kernel.org>
Sun, 6 Oct 2024 17:16:24 +0000 (19:16 +0200)
committerSteve French <stfrench@microsoft.com>
Wed, 26 Mar 2025 19:51:58 +0000 (14:51 -0500)
SMB1 protocol supports non-UNICODE (8-bit OEM character set) and
UNICODE (UTF-16) modes.

Linux SMB1 client implements both of them but currently does not allow to
choose non-UNICODE mode when SMB1 server announce UNICODE mode support.

This change adds a new mount option -o nounicode to disable UNICODE mode
and force usage of non-UNICODE (8-bit OEM character set) mode.

This allows to test non-UNICODE implementation of Linux SMB1 client against
any SMB1 server, including modern and recent Windows SMB1 server.

Signed-off-by: Pali Rohár <pali@kernel.org>
Signed-off-by: Steve French <stfrench@microsoft.com>
fs/smb/client/cifsfs.c
fs/smb/client/cifsglob.h
fs/smb/client/cifssmb.c
fs/smb/client/connect.c
fs/smb/client/fs_context.c
fs/smb/client/fs_context.h
fs/smb/client/sess.c
fs/smb/client/smb1ops.c

index 6a3bd652d251d31d1e48dedab0b84cc091bbb132..a08c42363ffc84ecae2446121573f1777841e3c7 100644 (file)
@@ -637,6 +637,10 @@ cifs_show_options(struct seq_file *s, struct dentry *root)
                                           cifs_sb->ctx->dir_mode);
        if (cifs_sb->ctx->iocharset)
                seq_printf(s, ",iocharset=%s", cifs_sb->ctx->iocharset);
+       if (tcon->ses->unicode == 0)
+               seq_puts(s, ",nounicode");
+       else if (tcon->ses->unicode == 1)
+               seq_puts(s, ",unicode");
        if (tcon->seal)
                seq_puts(s, ",seal");
        else if (tcon->ses->server->ignore_signature)
index cddeb2adbf4af598f5fca89e120661232bfb3d42..6ae170a2a04269022741e60cd3c188c55d0f6793 100644 (file)
@@ -653,6 +653,7 @@ struct smb_version_values {
        unsigned int    cap_unix;
        unsigned int    cap_nt_find;
        unsigned int    cap_large_files;
+       unsigned int    cap_unicode;
        __u16           signing_enabled;
        __u16           signing_required;
        size_t          create_lease_size;
@@ -1120,6 +1121,7 @@ struct cifs_ses {
        bool sign;              /* is signing required? */
        bool domainAuto:1;
        bool expired_pwd;  /* track if access denied or expired pwd so can know if need to update */
+       int unicode;
        unsigned int flags;
        __u16 session_flags;
        __u8 smb3signingkey[SMB3_SIGN_KEY_SIZE];
index 2cd6cb5ccea971f3d910bf3f0a025c6d411ae262..29dcb88392e5cd334b734333dae06b258a864eea 100644 (file)
@@ -437,7 +437,10 @@ CIFSSMBNegotiate(const unsigned int xid,
                return rc;
 
        pSMB->hdr.Mid = get_next_mid(server);
-       pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
+       pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
+
+       if (ses->unicode != 0)
+               pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
 
        if (should_set_ext_sec_flag(ses->sectype)) {
                cifs_dbg(FYI, "Requesting extended security\n");
index 2e23a4e047985f893cfe04a1d13df30ff376c2d6..d7bad2c3af37b77d5b502695bb92de1fb2ac666d 100644 (file)
@@ -2351,6 +2351,7 @@ retry_old_session:
        ses->cred_uid = ctx->cred_uid;
        ses->linux_uid = ctx->linux_uid;
 
+       ses->unicode = ctx->unicode;
        ses->sectype = ctx->sectype;
        ses->sign = ctx->sign;
 
@@ -4123,7 +4124,7 @@ cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
                   struct TCP_Server_Info *server,
                   struct nls_table *nls_info)
 {
-       int rc = -ENOSYS;
+       int rc = 0;
        struct TCP_Server_Info *pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
        struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&pserver->dstaddr;
        struct sockaddr_in *addr = (struct sockaddr_in *)&pserver->dstaddr;
@@ -4175,6 +4176,26 @@ cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
                if (!linuxExtEnabled)
                        ses->capabilities &= (~server->vals->cap_unix);
 
+               /*
+                * Check if the server supports specified encoding mode.
+                * Zero value in vals->cap_unicode indidcates that chosen
+                * protocol dialect does not support non-UNICODE mode.
+                */
+               if (ses->unicode == 1 && server->vals->cap_unicode != 0 &&
+                   !(server->capabilities & server->vals->cap_unicode)) {
+                       cifs_dbg(VFS, "Server does not support mounting in UNICODE mode\n");
+                       rc = -EOPNOTSUPP;
+               } else if (ses->unicode == 0 && server->vals->cap_unicode == 0) {
+                       cifs_dbg(VFS, "Server does not support mounting in non-UNICODE mode\n");
+                       rc = -EOPNOTSUPP;
+               } else if (ses->unicode == 0) {
+                       /*
+                        * When UNICODE mode was explicitly disabled then
+                        * do not announce client UNICODE capability.
+                        */
+                       ses->capabilities &= (~server->vals->cap_unicode);
+               }
+
                if (ses->auth_key.response) {
                        cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
                                 ses->auth_key.response);
@@ -4187,8 +4208,12 @@ cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
        cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
                 server->sec_mode, server->capabilities, server->timeAdj);
 
-       if (server->ops->sess_setup)
-               rc = server->ops->sess_setup(xid, ses, server, nls_info);
+       if (!rc) {
+               if (server->ops->sess_setup)
+                       rc = server->ops->sess_setup(xid, ses, server, nls_info);
+               else
+                       rc = -ENOSYS;
+       }
 
        if (rc) {
                cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
@@ -4258,6 +4283,7 @@ cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
        ctx->seal = master_tcon->seal;
        ctx->witness = master_tcon->use_witness;
        ctx->dfs_root_ses = master_tcon->ses->dfs_root_ses;
+       ctx->unicode = master_tcon->ses->unicode;
 
        rc = cifs_set_vol_auth(ctx, master_tcon->ses);
        if (rc) {
index 8c5a68ab9e236933a96ba824251c3ba34f43e88f..bdb762d398aff4852b27b3008818ee268764c3ee 100644 (file)
@@ -134,6 +134,7 @@ const struct fs_parameter_spec smb3_fs_parameters[] = {
        fsparam_flag("compress", Opt_compress),
        fsparam_flag("witness", Opt_witness),
        fsparam_flag_no("nativesocket", Opt_nativesocket),
+       fsparam_flag_no("unicode", Opt_unicode),
 
        /* Mount options which take uid or gid */
        fsparam_uid("backupuid", Opt_backupuid),
@@ -963,6 +964,10 @@ static int smb3_verify_reconfigure_ctx(struct fs_context *fc,
                cifs_errorf(fc, "can not change iocharset during remount\n");
                return -EINVAL;
        }
+       if (new_ctx->unicode != old_ctx->unicode) {
+               cifs_errorf(fc, "can not change unicode during remount\n");
+               return -EINVAL;
+       }
 
        return 0;
 }
@@ -1638,6 +1643,10 @@ static int smb3_fs_context_parse_param(struct fs_context *fc,
                ctx->witness = true;
                pr_warn_once("Witness protocol support is experimental\n");
                break;
+       case Opt_unicode:
+               ctx->unicode = !result.negated;
+               cifs_dbg(FYI, "unicode set to %d\n", ctx->unicode);
+               break;
        case Opt_rootfs:
 #ifndef CONFIG_CIFS_ROOT
                cifs_dbg(VFS, "rootfs support requires CONFIG_CIFS_ROOT config option\n");
@@ -1939,6 +1948,8 @@ int smb3_init_fs_context(struct fs_context *fc)
        ctx->symlink_type = CIFS_SYMLINK_TYPE_DEFAULT;
        ctx->nonativesocket = 0;
 
+       ctx->unicode = -1; /* autodetect, but prefer UNICODE mode */
+
 /*
  *     short int override_uid = -1;
  *     short int override_gid = -1;
index 881bfc08667e7d9d5b78516d0262f073b0ff4ce9..42c6b66c2c1afc9521a7a93088a63364ef1abc40 100644 (file)
@@ -135,6 +135,7 @@ enum cifs_param {
        Opt_witness,
        Opt_is_upcall_target_mount,
        Opt_is_upcall_target_application,
+       Opt_unicode,
 
        /* Mount options which take numeric value */
        Opt_backupuid,
@@ -306,6 +307,7 @@ struct smb3_fs_context {
        bool compress; /* enable SMB2 messages (READ/WRITE) de/compression */
        bool rootfs:1; /* if it's a SMB root file system */
        bool witness:1; /* use witness protocol */
+       int unicode;
        char *leaf_fullpath;
        struct cifs_ses *dfs_root_ses;
        bool dfs_automount:1; /* set for dfs automount only */
index be38176c694cf4ecafd5685719697553cd13c141..f2ca5963cd9d24d5ce753cfb759d1aa55a2939f1 100644 (file)
@@ -501,6 +501,7 @@ cifs_ses_add_channel(struct cifs_ses *ses,
        ctx->password = ses->password;
        ctx->sectype = ses->sectype;
        ctx->sign = ses->sign;
+       ctx->unicode = ses->unicode;
 
        /* UNC and paths */
        /* XXX: Use ses->server->hostname? */
index d6e2fb669c401f9181ff1c9f55c7f28d2b934876..8701484805cdcb77f52c4f9577faca40bf8e0567 100644 (file)
@@ -1170,6 +1170,7 @@ struct smb_version_values smb1_values = {
        .cap_unix = CAP_UNIX,
        .cap_nt_find = CAP_NT_SMBS | CAP_NT_FIND,
        .cap_large_files = CAP_LARGE_FILES,
+       .cap_unicode = CAP_UNICODE,
        .signing_enabled = SECMODE_SIGN_ENABLED,
        .signing_required = SECMODE_SIGN_REQUIRED,
 };