Skip to content

Commit 5c1acf3

Browse files
Paulo Alcantarasmfrench
authored andcommitted
cifs: fix regression when mounting shares with prefix paths
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]>
1 parent 5b2abda commit 5c1acf3

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
@@ -863,13 +863,7 @@ cifs_smb3_do_mount(struct file_system_type *fs_type,
863863
goto out;
864864
}
865865

866-
/* cifs_setup_volume_info->smb3_parse_devname() redups UNC & prepath */
867-
kfree(cifs_sb->ctx->UNC);
868-
cifs_sb->ctx->UNC = NULL;
869-
kfree(cifs_sb->ctx->prepath);
870-
cifs_sb->ctx->prepath = NULL;
871-
872-
rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, old_ctx->UNC);
866+
rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, NULL);
873867
if (rc) {
874868
root = ERR_PTR(rc);
875869
goto out;

fs/cifs/connect.c

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

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

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

3159-
cifs_dbg(FYI, "%s: mntopts=%s\n", __func__, mntopts);
31603166
rc = smb3_parse_opt(mntopts, "ip", &ip);
3161-
if (!rc && !cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip,
3162-
strlen(ip))) {
3167+
if (rc) {
3168+
cifs_dbg(VFS, "%s: failed to parse ip options: %d\n", __func__, rc);
3169+
return rc;
3170+
}
3171+
3172+
rc = cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, strlen(ip));
3173+
kfree(ip);
3174+
if (!rc) {
31633175
cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
31643176
return -EINVAL;
31653177
}
@@ -3179,7 +3191,7 @@ cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const c
31793191
return -EINVAL;
31803192
}
31813193

3182-
return rc;
3194+
return 0;
31833195
}
31843196

31853197
static int

fs/cifs/fs_context.c

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

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

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

0 commit comments

Comments
 (0)