Skip to contentSkip to conversation

cifs: fix regression when mounting shares with prefix paths

PATCH Superseded Paulo Alcantara 3 participants 7 messages Apr 30, 2021 – May 4, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cifs: fix regression when mounting shares with prefix paths

The commit 315db9a05b7a ("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 315db9a05b7a ("cifs: fix leak in cifs_smb3_do_mount() ctx")
---
 fs/cifs/cifsfs.c     |  8 +-------
 fs/cifs/connect.c    | 25 +++++++++++++++++--------
 fs/cifs/fs_context.c |  4 ++++
 3 files changed, 22 insertions(+), 15 deletions(-)
fs/cifs/cifsfs.c +1 -7
index 8a6894577697..d7ea9c5fe0f8 100644
@@ -863,13 +863,7 @@ cifs_smb3_do_mount(struct file_system_type *fs_type,
 		goto out;
 	}
 
-	/* cifs_setup_volume_info->smb3_parse_devname() redups UNC & prepath */
-	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);
+	rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, NULL);
 	if (rc) {
 		root = ERR_PTR(rc);
 		goto out;
fs/cifs/connect.c +17 -8
index becd5f807787..04a06e22e715 100644
@@ -3159,19 +3159,28 @@ static int do_dfs_failover(const char *path, const char *full_path, struct cifs_
 int
 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) {
 		char *ip;
 
-		cifs_dbg(FYI, "%s: mntopts=%s\n", __func__, mntopts);
 		rc = smb3_parse_opt(mntopts, "ip", &ip);
-		if (!rc && !cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip,
-						 strlen(ip))) {
-			cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
-			return -EINVAL;
+		if (!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__);
+				return -EINVAL;
+			}
 		}
 	}
 
@@ -3189,7 +3198,7 @@ cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const c
 		return -EINVAL;
 	}
 
-	return rc;
+	return 0;
 }
 
 static int
fs/cifs/fs_context.c +4 -0
index 1d6e0e15b034..3bcf881c3ae9 100644
@@ -476,6 +476,7 @@ smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx)
 
 	/* move "pos" up to delimiter or NULL */
 	pos += len;
+	kfree(ctx->UNC);
 	ctx->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
 	if (!ctx->UNC)
 		return -ENOMEM;
@@ -486,6 +487,9 @@ smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx)
 	if (*pos == '/' || *pos == '\\')
 		pos++;
 
+	kfree(ctx->prepath);
+	ctx->prepath = NULL;
+
 	/* If pos is NULL then no prepath */
 	if (!*pos)
 		return 0;
Signature · 1 line
2.31.1
#1 lore raw
Quoted 3 lines · Paulo Alcantara

The commit 315db9a05b7a ("cifs: fix leak in cifs_smb3_do_mount() ctx") revealed an existing bug when mounting shares that contain a prefix path or DFS links.

Sorry for the mess. One question...

...

Quoted 20 lines
fs/cifs/connect.c +7 -5
 	if (mntopts) {
 		char *ip;
 
-		cifs_dbg(FYI, "%s: mntopts=%s\n", __func__, mntopts);
 		rc = smb3_parse_opt(mntopts, "ip", &ip);
-		if (!rc && !cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip,
-						 strlen(ip))) {
-			cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
-			return -EINVAL;
+		if (!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__);
+				return -EINVAL;
+			}
 		}
 	}

fs/cifs/connect.c cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const c · line 3202

Quoted 6 lines
fs/cifs/connect.c +1 -1
@@ -3189,7 +3198,7 @@ cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const c
 		return -EINVAL;
 	}
 
-	return rc;
+	return 0;
 }

It seems that smb3_parse_opt() errors will no longer be propagated here. Is that intentional?

Cheers, David

#2 lore raw
Quoted 32 lines · David Disseldorp
Quoted 3 lines · Paulo Alcantara

The commit 315db9a05b7a ("cifs: fix leak in cifs_smb3_do_mount() ctx") revealed an existing bug when mounting shares that contain a prefix path or DFS links.

Sorry for the mess. One question...

...

Quoted 23 lines
fs/cifs/connect.c +7 -5
 	if (mntopts) {
 		char *ip;
 
-		cifs_dbg(FYI, "%s: mntopts=%s\n", __func__, mntopts);
 		rc = smb3_parse_opt(mntopts, "ip", &ip);
-		if (!rc && !cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip,
-						 strlen(ip))) {
-			cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
-			return -EINVAL;
+		if (!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__);
+				return -EINVAL;
+			}
 		}
 	}
@@ -3189,7 +3198,7 @@ cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const c
 		return -EINVAL;
 	}
 
Quoted 6 lines

fs/cifs/connect.c cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const c · line 3202

Quoted 3 lines
fs/cifs/connect.c +1 -1
-	return rc;
+	return 0;
 }

It seems that smb3_parse_opt() errors will no longer be propagated here. Is that intentional?

That was a mistake, actually. Will fix it in v2. Thanks!

#3 lore raw

PATCH v2 1/1 cifs: fix regression when mounting shares with prefix paths

In reply to Paulo Alcantara

The commit 315db9a05b7a ("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 315db9a05b7a ("cifs: fix leak in cifs_smb3_do_mount() ctx")
---
 fs/cifs/cifsfs.c     |  8 +-------
 fs/cifs/connect.c    | 24 ++++++++++++++++++------
 fs/cifs/fs_context.c |  4 ++++
 3 files changed, 23 insertions(+), 13 deletions(-)
fs/cifs/cifsfs.c +1 -7
index 8a6894577697..d7ea9c5fe0f8 100644
@@ -863,13 +863,7 @@ cifs_smb3_do_mount(struct file_system_type *fs_type,
 		goto out;
 	}
 
-	/* cifs_setup_volume_info->smb3_parse_devname() redups UNC & prepath */
-	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);
+	rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, NULL);
 	if (rc) {
 		root = ERR_PTR(rc);
 		goto out;
fs/cifs/connect.c +18 -6
index becd5f807787..4ce6fc24cae1 100644
@@ -3159,17 +3159,29 @@ static int do_dfs_failover(const char *path, const char *full_path, struct cifs_
 int
 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) {
 		char *ip;
 
-		cifs_dbg(FYI, "%s: mntopts=%s\n", __func__, mntopts);
 		rc = smb3_parse_opt(mntopts, "ip", &ip);
-		if (!rc && !cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip,
-						 strlen(ip))) {
+		if (rc) {
+			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__);
 			return -EINVAL;
 		}
@@ -3189,7 +3201,7 @@ cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const c
 		return -EINVAL;
 	}
 
-	return rc;
+	return 0;
 }
 
 static int
fs/cifs/fs_context.c +4 -0
index 1d6e0e15b034..3bcf881c3ae9 100644
@@ -476,6 +476,7 @@ smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx)
 
 	/* move "pos" up to delimiter or NULL */
 	pos += len;
+	kfree(ctx->UNC);
 	ctx->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
 	if (!ctx->UNC)
 		return -ENOMEM;
@@ -486,6 +487,9 @@ smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx)
 	if (*pos == '/' || *pos == '\\')
 		pos++;
 
+	kfree(ctx->prepath);
+	ctx->prepath = NULL;
+
 	/* If pos is NULL then no prepath */
 	if (!*pos)
 		return 0;
Signature · 1 line
2.31.1
Acked-by on v2 1/1
#4 lore raw
Quoted 30 lines · Paulo Alcantara

The commit 315db9a05b7a ("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 315db9a05b7a ("cifs: fix leak in cifs_smb3_do_mount() ctx")
---
 fs/cifs/cifsfs.c     |  8 +-------
 fs/cifs/connect.c    | 24 ++++++++++++++++++------
 fs/cifs/fs_context.c |  4 ++++
 3 files changed, 23 insertions(+), 13 deletions(-)
fs/cifs/cifsfs.c +0 -0
index 8a6894577697..d7ea9c5fe0f8 100644
@@ -863,13 +863,7 @@ cifs_smb3_do_mount(struct file_system_type *fs_type,
 		goto out;

fs/cifs/cifsfs.c cifs_smb3_do_mount(struct file_system_type *fs_type, · line 866

Quoted 10 lines
fs/cifs/cifsfs.c +1 -7
 	}
 
-	/* cifs_setup_volume_info->smb3_parse_devname() redups UNC & prepath */
-	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);
+	rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, NULL);

IIUC, with the new behaviour, this call becomes pretty much an

	if (!cifs_sb->ctx->username)
		root = ERR_PTR(-EINVAL);

So it might be worth simplifying this further. Either way:

Cheers, David

#5 lore raw
Quoted 7 lines · David Disseldorp

fs/cifs/cifsfs.c cifs_smb3_do_mount(struct file_system_type *fs_type, · line 866

Quoted 2 lines · Paulo Alcantara
fs/cifs/cifsfs.c +1 -1
-	rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, old_ctx->UNC);
+	rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, NULL);

IIUC, with the new behaviour, this call becomes pretty much an

	if (!cifs_sb->ctx->username)
		root = ERR_PTR(-EINVAL);

Yeah. We should get rid of the damn thing at some point. I've just tried to keep the logic we currently have in fs/cifs/connect.c by composing new options based upon the chased DFS referrals.

Thanks for pointing it out!

#6 lore raw
In reply to David Disseldorp

added acked-by and merged into cifs-2.6.git for-next

Quoted 52 lines · David Disseldorp
Quoted 30 lines · Paulo Alcantara

The commit 315db9a05b7a ("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 315db9a05b7a ("cifs: fix leak in cifs_smb3_do_mount() ctx")
---
 fs/cifs/cifsfs.c     |  8 +-------
 fs/cifs/connect.c    | 24 ++++++++++++++++++------
 fs/cifs/fs_context.c |  4 ++++
 3 files changed, 23 insertions(+), 13 deletions(-)
fs/cifs/cifsfs.c +0 -0
index 8a6894577697..d7ea9c5fe0f8 100644
@@ -863,13 +863,7 @@ cifs_smb3_do_mount(struct file_system_type *fs_type,
              goto out;

fs/cifs/cifsfs.c cifs_smb3_do_mount(struct file_system_type *fs_type, · line 866

Quoted 10 lines
fs/cifs/cifsfs.c +1 -7
      }
 
-     /* cifs_setup_volume_info->smb3_parse_devname() redups UNC & prepath */
-     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);
+     rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, NULL);

IIUC, with the new behaviour, this call becomes pretty much an

        if (!cifs_sb->ctx->username)
                root = ERR_PTR(-EINVAL);

So it might be worth simplifying this further. Either way:

Cheers, David

Signature · 3 lines
Thanks,

Steve