Skip to content

Commit 3363da8

Browse files
pcacjrsmfrench
authored andcommitted
smb: client: fix native SMB symlink traversal
We've seen customers having shares mounted in paths like /??/C:/ or /??/UNC/foo.example.com/share in order to get their native SMB symlinks successfully followed from different mounts. After commit 12b466e ("cifs: Fix creating and resolving absolute NT-style symlinks"), the client would then convert absolute paths from "/??/C:/" to "/mnt/c/" by default. The absolute paths would vary depending on the value of symlinkroot= mount option. Fix this by restoring old behavior of not trying to convert absolute paths by default. Only do this if symlinkroot= was _explicitly_ set. Before patch: $ mount.cifs //w22-fs0/test2 /mnt/1 -o vers=3.1.1,username=xxx,password=yyy $ ls -l /mnt/1/symlink2 lrwxr-xr-x 1 root root 15 Jun 20 14:22 /mnt/1/symlink2 -> /mnt/c/testfile $ mkdir -p /??/C:; echo foo > //??/C:/testfile $ cat /mnt/1/symlink2 cat: /mnt/1/symlink2: No such file or directory After patch: $ mount.cifs //w22-fs0/test2 /mnt/1 -o vers=3.1.1,username=xxx,password=yyy $ ls -l /mnt/1/symlink2 lrwxr-xr-x 1 root root 15 Jun 20 14:22 /mnt/1/symlink2 -> '/??/C:/testfile' $ mkdir -p /??/C:; echo foo > //??/C:/testfile $ cat /mnt/1/symlink2 foo Cc: [email protected] Reported-by: Pierguido Lambri <[email protected]> Cc: David Howells <[email protected]> Cc: Stefan Metzmacher <[email protected]> Fixes: 12b466e ("cifs: Fix creating and resolving absolute NT-style symlinks") Signed-off-by: Paulo Alcantara (Red Hat) <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent 266b5d0 commit 3363da8

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

fs/smb/client/fs_context.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,10 +1824,14 @@ static int smb3_fs_context_parse_param(struct fs_context *fc,
18241824
cifs_errorf(fc, "symlinkroot mount options must be absolute path\n");
18251825
goto cifs_parse_mount_err;
18261826
}
1827-
kfree(ctx->symlinkroot);
1828-
ctx->symlinkroot = kstrdup(param->string, GFP_KERNEL);
1829-
if (!ctx->symlinkroot)
1827+
if (strnlen(param->string, PATH_MAX) == PATH_MAX) {
1828+
cifs_errorf(fc, "symlinkroot path too long (max path length: %u)\n",
1829+
PATH_MAX - 1);
18301830
goto cifs_parse_mount_err;
1831+
}
1832+
kfree(ctx->symlinkroot);
1833+
ctx->symlinkroot = param->string;
1834+
param->string = NULL;
18311835
break;
18321836
}
18331837
/* case Opt_ignore: - is ignored as expected ... */
@@ -1837,13 +1841,6 @@ static int smb3_fs_context_parse_param(struct fs_context *fc,
18371841
goto cifs_parse_mount_err;
18381842
}
18391843

1840-
/*
1841-
* By default resolve all native absolute symlinks relative to "/mnt/".
1842-
* Same default has drvfs driver running in WSL for resolving SMB shares.
1843-
*/
1844-
if (!ctx->symlinkroot)
1845-
ctx->symlinkroot = kstrdup("/mnt/", GFP_KERNEL);
1846-
18471844
return 0;
18481845

18491846
cifs_parse_mount_err:

fs/smb/client/reparse.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ static int create_native_symlink(const unsigned int xid, struct inode *inode,
5757
struct reparse_symlink_data_buffer *buf = NULL;
5858
struct cifs_open_info_data data = {};
5959
struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
60+
const char *symroot = cifs_sb->ctx->symlinkroot;
6061
struct inode *new;
6162
struct kvec iov;
6263
__le16 *path = NULL;
@@ -82,7 +83,8 @@ static int create_native_symlink(const unsigned int xid, struct inode *inode,
8283
.symlink_target = symlink_target,
8384
};
8485

85-
if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/') {
86+
if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) &&
87+
symroot && symname[0] == '/') {
8688
/*
8789
* This is a request to create an absolute symlink on the server
8890
* which does not support POSIX paths, and expects symlink in
@@ -92,7 +94,7 @@ static int create_native_symlink(const unsigned int xid, struct inode *inode,
9294
* ensure compatibility of this symlink stored in absolute form
9395
* on the SMB server.
9496
*/
95-
if (!strstarts(symname, cifs_sb->ctx->symlinkroot)) {
97+
if (!strstarts(symname, symroot)) {
9698
/*
9799
* If the absolute Linux symlink target path is not
98100
* inside "symlinkroot" location then there is no way
@@ -101,12 +103,12 @@ static int create_native_symlink(const unsigned int xid, struct inode *inode,
101103
cifs_dbg(VFS,
102104
"absolute symlink '%s' cannot be converted to NT format "
103105
"because it is outside of symlinkroot='%s'\n",
104-
symname, cifs_sb->ctx->symlinkroot);
106+
symname, symroot);
105107
rc = -EINVAL;
106108
goto out;
107109
}
108-
len = strlen(cifs_sb->ctx->symlinkroot);
109-
if (cifs_sb->ctx->symlinkroot[len-1] != '/')
110+
len = strlen(symroot);
111+
if (symroot[len - 1] != '/')
110112
len++;
111113
if (symname[len] >= 'a' && symname[len] <= 'z' &&
112114
(symname[len+1] == '/' || symname[len+1] == '\0')) {
@@ -782,6 +784,7 @@ int smb2_parse_native_symlink(char **target, const char *buf, unsigned int len,
782784
const char *full_path,
783785
struct cifs_sb_info *cifs_sb)
784786
{
787+
const char *symroot = cifs_sb->ctx->symlinkroot;
785788
char sep = CIFS_DIR_SEP(cifs_sb);
786789
char *linux_target = NULL;
787790
char *smb_target = NULL;
@@ -815,7 +818,8 @@ int smb2_parse_native_symlink(char **target, const char *buf, unsigned int len,
815818
goto out;
816819
}
817820

818-
if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && !relative) {
821+
if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) &&
822+
symroot && !relative) {
819823
/*
820824
* This is an absolute symlink from the server which does not
821825
* support POSIX paths, so the symlink is in NT-style path.
@@ -907,15 +911,15 @@ int smb2_parse_native_symlink(char **target, const char *buf, unsigned int len,
907911
}
908912

909913
abs_path_len = strlen(abs_path)+1;
910-
symlinkroot_len = strlen(cifs_sb->ctx->symlinkroot);
911-
if (cifs_sb->ctx->symlinkroot[symlinkroot_len-1] == '/')
914+
symlinkroot_len = strlen(symroot);
915+
if (symroot[symlinkroot_len - 1] == '/')
912916
symlinkroot_len--;
913917
linux_target = kmalloc(symlinkroot_len + 1 + abs_path_len, GFP_KERNEL);
914918
if (!linux_target) {
915919
rc = -ENOMEM;
916920
goto out;
917921
}
918-
memcpy(linux_target, cifs_sb->ctx->symlinkroot, symlinkroot_len);
922+
memcpy(linux_target, symroot, symlinkroot_len);
919923
linux_target[symlinkroot_len] = '/';
920924
memcpy(linux_target + symlinkroot_len + 1, abs_path, abs_path_len);
921925
} else if (smb_target[0] == sep && relative) {

0 commit comments

Comments
 (0)