Skip to content

Commit ee3b051

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 eb87796 commit ee3b051

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
@@ -834,13 +834,7 @@ cifs_smb3_do_mount(struct file_system_type *fs_type,
834834
goto out;
835835
}
836836

837-
/* cifs_setup_volume_info->smb3_parse_devname() redups UNC & prepath */
838-
kfree(cifs_sb->ctx->UNC);
839-
cifs_sb->ctx->UNC = NULL;
840-
kfree(cifs_sb->ctx->prepath);
841-
cifs_sb->ctx->prepath = NULL;
842-
843-
rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, old_ctx->UNC);
837+
rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, NULL);
844838
if (rc) {
845839
root = ERR_PTR(rc);
846840
goto out;

fs/cifs/connect.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3176,17 +3176,29 @@ static int do_dfs_failover(const char *path, const char *full_path, struct cifs_
31763176
int
31773177
cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname)
31783178
{
3179-
int rc = 0;
3179+
int rc;
31803180

3181-
smb3_parse_devname(devname, ctx);
3181+
if (devname) {
3182+
cifs_dbg(FYI, "%s: devname=%s\n", __func__, devname);
3183+
rc = smb3_parse_devname(devname, ctx);
3184+
if (rc) {
3185+
cifs_dbg(VFS, "%s: failed to parse %s: %d\n", __func__, devname, rc);
3186+
return rc;
3187+
}
3188+
}
31823189

31833190
if (mntopts) {
31843191
char *ip;
31853192

3186-
cifs_dbg(FYI, "%s: mntopts=%s\n", __func__, mntopts);
31873193
rc = smb3_parse_opt(mntopts, "ip", &ip);
3188-
if (!rc && !cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip,
3189-
strlen(ip))) {
3194+
if (rc) {
3195+
cifs_dbg(VFS, "%s: failed to parse ip options: %d\n", __func__, rc);
3196+
return rc;
3197+
}
3198+
3199+
rc = cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, strlen(ip));
3200+
kfree(ip);
3201+
if (!rc) {
31903202
cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
31913203
return -EINVAL;
31923204
}
@@ -3206,7 +3218,7 @@ cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const c
32063218
return -EINVAL;
32073219
}
32083220

3209-
return rc;
3221+
return 0;
32103222
}
32113223

32123224
static int

fs/cifs/fs_context.c

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

476476
/* move "pos" up to delimiter or NULL */
477477
pos += len;
478+
kfree(ctx->UNC);
478479
ctx->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
479480
if (!ctx->UNC)
480481
return -ENOMEM;
@@ -485,6 +486,9 @@ smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx)
485486
if (*pos == '/' || *pos == '\\')
486487
pos++;
487488

489+
kfree(ctx->prepath);
490+
ctx->prepath = NULL;
491+
488492
/* If pos is NULL then no prepath */
489493
if (!*pos)
490494
return 0;

0 commit comments

Comments
 (0)