Skip to content

Commit e69ceb7

Browse files
pcacjrgregkh
authored andcommitted
cifs: fix regression when mounting shares with prefix paths
commit 5c1acf3 upstream. The commit 315db9a ("cifs: fix leak in cifs_smb3_do_mount() ctx") revealed an existing bug when mounting shares that contain a prefix path or DFS links. cifs_setup_volume_info() requires the @devname to contain the full path (UNC + prefix) to update the fs context with the new UNC and prepath values, however we were passing only the UNC path (old_ctx->UNC) in @device thus discarding any prefix paths. Instead of concatenating both old_ctx->{UNC,prepath} and pass it in @devname, just keep the dup'ed values of UNC and prepath in cifs_sb->ctx after calling smb3_fs_context_dup(), and fix smb3_parse_devname() to correctly parse and not leak the new UNC and prefix paths. Cc: <[email protected]> # v5.11+ Fixes: 315db9a ("cifs: fix leak in cifs_smb3_do_mount() ctx") Signed-off-by: Paulo Alcantara (SUSE) <[email protected]> Acked-by: David Disseldorp <[email protected]> Signed-off-by: Steve French <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 32f21ef commit e69ceb7

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

fs/cifs/cifsfs.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -823,13 +823,7 @@ cifs_smb3_do_mount(struct file_system_type *fs_type,
823823
goto out;
824824
}
825825

826-
/* cifs_setup_volume_info->smb3_parse_devname() redups UNC & prepath */
827-
kfree(cifs_sb->ctx->UNC);
828-
cifs_sb->ctx->UNC = NULL;
829-
kfree(cifs_sb->ctx->prepath);
830-
cifs_sb->ctx->prepath = NULL;
831-
832-
rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, old_ctx->UNC);
826+
rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, NULL);
833827
if (rc) {
834828
root = ERR_PTR(rc);
835829
goto out;

fs/cifs/connect.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,17 +3150,29 @@ static int do_dfs_failover(const char *path, const char *full_path, struct cifs_
31503150
int
31513151
cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname)
31523152
{
3153-
int rc = 0;
3153+
int rc;
31543154

3155-
smb3_parse_devname(devname, ctx);
3155+
if (devname) {
3156+
cifs_dbg(FYI, "%s: devname=%s\n", __func__, devname);
3157+
rc = smb3_parse_devname(devname, ctx);
3158+
if (rc) {
3159+
cifs_dbg(VFS, "%s: failed to parse %s: %d\n", __func__, devname, rc);
3160+
return rc;
3161+
}
3162+
}
31563163

31573164
if (mntopts) {
31583165
char *ip;
31593166

3160-
cifs_dbg(FYI, "%s: mntopts=%s\n", __func__, mntopts);
31613167
rc = smb3_parse_opt(mntopts, "ip", &ip);
3162-
if (!rc && !cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip,
3163-
strlen(ip))) {
3168+
if (rc) {
3169+
cifs_dbg(VFS, "%s: failed to parse ip options: %d\n", __func__, rc);
3170+
return rc;
3171+
}
3172+
3173+
rc = cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, strlen(ip));
3174+
kfree(ip);
3175+
if (!rc) {
31643176
cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
31653177
return -EINVAL;
31663178
}
@@ -3180,7 +3192,7 @@ cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const c
31803192
return -EINVAL;
31813193
}
31823194

3183-
return rc;
3195+
return 0;
31843196
}
31853197

31863198
static int

fs/cifs/fs_context.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx)
473473

474474
/* move "pos" up to delimiter or NULL */
475475
pos += len;
476+
kfree(ctx->UNC);
476477
ctx->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
477478
if (!ctx->UNC)
478479
return -ENOMEM;
@@ -483,6 +484,9 @@ smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx)
483484
if (*pos == '/' || *pos == '\\')
484485
pos++;
485486

487+
kfree(ctx->prepath);
488+
ctx->prepath = NULL;
489+
486490
/* If pos is NULL then no prepath */
487491
if (!*pos)
488492
return 0;

0 commit comments

Comments
 (0)