Commit 5c1acf3f authored by Paulo Alcantara's avatar Paulo Alcantara Committed by Steve French

cifs: fix regression when mounting shares with prefix paths

The commit 315db9a0 ("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: <stable@vger.kernel.org> # v5.11+
Fixes: 315db9a0 ("cifs: fix leak in cifs_smb3_do_mount() ctx")
Signed-off-by: default avatarPaulo Alcantara (SUSE) <pc@cjr.nz>
Acked-by: default avatarDavid Disseldorp <ddiss@suse.de>
Signed-off-by: default avatarSteve French <stfrench@microsoft.com>
parent 5b2abdaf
...@@ -863,13 +863,7 @@ cifs_smb3_do_mount(struct file_system_type *fs_type, ...@@ -863,13 +863,7 @@ cifs_smb3_do_mount(struct file_system_type *fs_type,
goto out; goto out;
} }
/* cifs_setup_volume_info->smb3_parse_devname() redups UNC & prepath */ rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, NULL);
kfree(cifs_sb->ctx->UNC);
cifs_sb->ctx->UNC = NULL;
kfree(cifs_sb->ctx->prepath);
cifs_sb->ctx->prepath = NULL;
rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, old_ctx->UNC);
if (rc) { if (rc) {
root = ERR_PTR(rc); root = ERR_PTR(rc);
goto out; goto out;
......
...@@ -3149,17 +3149,29 @@ static int do_dfs_failover(const char *path, const char *full_path, struct cifs_ ...@@ -3149,17 +3149,29 @@ static int do_dfs_failover(const char *path, const char *full_path, struct cifs_
int int
cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname) cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname)
{ {
int rc = 0; int rc;
smb3_parse_devname(devname, ctx); if (devname) {
cifs_dbg(FYI, "%s: devname=%s\n", __func__, devname);
rc = smb3_parse_devname(devname, ctx);
if (rc) {
cifs_dbg(VFS, "%s: failed to parse %s: %d\n", __func__, devname, rc);
return rc;
}
}
if (mntopts) { if (mntopts) {
char *ip; char *ip;
cifs_dbg(FYI, "%s: mntopts=%s\n", __func__, mntopts);
rc = smb3_parse_opt(mntopts, "ip", &ip); rc = smb3_parse_opt(mntopts, "ip", &ip);
if (!rc && !cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, if (rc) {
strlen(ip))) { cifs_dbg(VFS, "%s: failed to parse ip options: %d\n", __func__, rc);
return rc;
}
rc = cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, strlen(ip));
kfree(ip);
if (!rc) {
cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__); cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
return -EINVAL; return -EINVAL;
} }
...@@ -3179,7 +3191,7 @@ cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const c ...@@ -3179,7 +3191,7 @@ cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const c
return -EINVAL; return -EINVAL;
} }
return rc; return 0;
} }
static int static int
......
...@@ -476,6 +476,7 @@ smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx) ...@@ -476,6 +476,7 @@ smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx)
/* move "pos" up to delimiter or NULL */ /* move "pos" up to delimiter or NULL */
pos += len; pos += len;
kfree(ctx->UNC);
ctx->UNC = kstrndup(devname, pos - devname, GFP_KERNEL); ctx->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
if (!ctx->UNC) if (!ctx->UNC)
return -ENOMEM; return -ENOMEM;
...@@ -486,6 +487,9 @@ smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx) ...@@ -486,6 +487,9 @@ smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx)
if (*pos == '/' || *pos == '\\') if (*pos == '/' || *pos == '\\')
pos++; pos++;
kfree(ctx->prepath);
ctx->prepath = NULL;
/* If pos is NULL then no prepath */ /* If pos is NULL then no prepath */
if (!*pos) if (!*pos)
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment