public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Pali Rohár" <pali@kernel.org>
To: Steve French <sfrench@samba.org>,
	Paulo Alcantara <pc@manguebit.com>,
	Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 4/6] cifs: Fix parsing native symlinks directory/file type
Date: Sat,  5 Oct 2024 16:02:58 +0200	[thread overview]
Message-ID: <20241005140300.19416-5-pali@kernel.org> (raw)
In-Reply-To: <20241005140300.19416-1-pali@kernel.org>

As SMB protocol distinguish between symlink to directory and symlink to
file, add some mechanism to disallow resolving incompatible types.

When SMB symlink is of the directory type, ensure that its target path ends
with slash. This forces Linux to not allow resolving such symlink to file.

And when SMB symlink is of the file type and its target path ends with
slash then returns an error as such symlink is unresolvable. Such symlink
always points to invalid location as file cannot end with slash.

As POSIX server does not distinguish between symlinks to file and symlink
directory, do not apply this change for symlinks from POSIX SMB server. For
POSIX SMB servers, this change does nothing.

This mimics Windows behavior of native SMB symlinks.

Signed-off-by: Pali Rohár <pali@kernel.org>
---
 fs/smb/client/inode.c     |  5 ++++
 fs/smb/client/smb2file.c  | 55 +++++++++++++++++++++++++++++++++++++++
 fs/smb/client/smb2inode.c |  4 +++
 fs/smb/client/smb2proto.h |  1 +
 4 files changed, 65 insertions(+)

diff --git a/fs/smb/client/inode.c b/fs/smb/client/inode.c
index 0fe54b2d2561..aa38a3935f8f 100644
--- a/fs/smb/client/inode.c
+++ b/fs/smb/client/inode.c
@@ -1110,6 +1110,11 @@ static int reparse_info_to_fattr(struct cifs_open_info_data *data,
 							      full_path,
 							      iov, data);
 		}
+
+		if (data->reparse.tag == IO_REPARSE_TAG_SYMLINK && !rc) {
+			bool directory = le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY;
+			rc = smb2_fix_symlink_target_type(&data->symlink_target, directory, cifs_sb);
+		}
 		break;
 	}
 
diff --git a/fs/smb/client/smb2file.c b/fs/smb/client/smb2file.c
index dc52995f5591..149449d9c1c0 100644
--- a/fs/smb/client/smb2file.c
+++ b/fs/smb/client/smb2file.c
@@ -63,6 +63,56 @@ static struct smb2_symlink_err_rsp *symlink_data(const struct kvec *iov)
 	return sym;
 }
 
+int smb2_fix_symlink_target_type(char **target, bool directory, struct cifs_sb_info *cifs_sb)
+{
+	char *buf;
+	int len;
+
+	/*
+	 * POSIX server does not distinguish between symlinks to file and
+	 * symlink directory. So nothing is needed to fix on the client side.
+	 */
+	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
+		return 0;
+
+	len = strlen(*target);
+	if (!len)
+		return -EIO;
+
+	/*
+	 * If this is directory symlink and it does not have trailing slash then
+	 * append it. Trailing slash simulates Windows/SMB behavior which do not
+	 * allow resolving directory symlink to file.
+	 */
+	if (directory && (*target)[len-1] != '/') {
+		buf = kzalloc(len+2, GFP_KERNEL);
+		if (!buf)
+			return -ENOMEM;
+		memcpy(buf, *target, len);
+		buf[len] = '/';
+		kfree(*target);
+		*target = buf;
+	}
+
+	/*
+	 * If this is a symlink which points to file name with trailing slash,
+	 * or to file named "." or file named ".." then this symlink cannot be
+	 * resolved on Linux because Linux does not allow files with such names.
+	 * So return an error to prevent resolving this file type symlink to
+	 * directory, as it do not point to directory at all.
+	 */
+	if (!directory) {
+		const char *basename = kbasename(*target);
+		int basename_len = strlen(basename);
+		if (basename_len == 0 || /* symname ends with slash */
+		    (basename_len == 1 && basename[0] == '.') || /* last component is "." */
+		    (basename_len == 2 && basename[0] == '.' && basename[1] == '.')) /* or ".." */
+			return -EIO;
+	}
+
+	return 0;
+}
+
 int smb2_parse_symlink_response(struct cifs_sb_info *cifs_sb, const struct kvec *iov,
 				const char *full_path, char **path)
 {
@@ -133,6 +183,11 @@ int smb2_open_file(const unsigned int xid, struct cifs_open_parms *oparms, __u32
 					       NULL, NULL, NULL);
 				oparms->create_options &= ~OPEN_REPARSE_POINT;
 			}
+			if (!rc) {
+				bool directory = le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY;
+				rc = smb2_fix_symlink_target_type(&data->symlink_target,
+								  directory, oparms->cifs_sb);
+			}
 		}
 	}
 
diff --git a/fs/smb/client/smb2inode.c b/fs/smb/client/smb2inode.c
index 9a28a30ec1a3..06bb6f7fbf0f 100644
--- a/fs/smb/client/smb2inode.c
+++ b/fs/smb/client/smb2inode.c
@@ -960,6 +960,10 @@ int smb2_query_path_info(const unsigned int xid,
 		rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
 				      &oparms, in_iov, cmds, num_cmds,
 				      cfile, NULL, NULL, NULL);
+		if (data->reparse.tag == IO_REPARSE_TAG_SYMLINK && !rc) {
+			bool directory = le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY;
+			rc = smb2_fix_symlink_target_type(&data->symlink_target, directory, cifs_sb);
+		}
 		break;
 	case -EREMOTE:
 		break;
diff --git a/fs/smb/client/smb2proto.h b/fs/smb/client/smb2proto.h
index aa01ae234732..1828b825c7d3 100644
--- a/fs/smb/client/smb2proto.h
+++ b/fs/smb/client/smb2proto.h
@@ -113,6 +113,7 @@ extern int smb3_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
 			  struct cifs_sb_info *cifs_sb,
 			  const unsigned char *path, char *pbuf,
 			  unsigned int *pbytes_read);
+int smb2_fix_symlink_target_type(char **target, bool directory, struct cifs_sb_info *cifs_sb);
 int smb2_parse_native_symlink(char **target, const char *buf, unsigned int len,
 			      bool unicode, bool relative,
 			      const char *full_path,
-- 
2.20.1


  parent reply	other threads:[~2024-10-05 14:03 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-29 18:50 [PATCH 0/7] cifs: Improve support for native SMB symlinks Pali Rohár
2024-09-29 18:50 ` [PATCH 1/7] cifs: Rename smb2_get_reparse_inode to smb2_create_reparse_inode Pali Rohár
2024-09-29 21:16   ` Steve French
2024-09-29 22:05     ` Pali Rohár
2024-09-29 18:50 ` [PATCH 2/7] cifs: Improve creating native symlinks pointing to directory Pali Rohár
2024-09-29 21:54   ` Steve French
2024-09-29 22:11     ` Pali Rohár
2024-09-30 15:09   ` Paulo Alcantara
2024-09-30 17:17     ` Pali Rohár
2024-10-05 14:08       ` Pali Rohár
2024-09-29 18:50 ` [PATCH 3/7] cifs: Fix creating native symlinks pointing to current or parent directory Pali Rohár
2024-09-29 18:50 ` [PATCH 4/7] cifs: Fix parsing native symlinks relative to the export Pali Rohár
2024-09-29 18:50 ` [PATCH 5/7] cifs: Fix parsing native symlinks directory/file type Pali Rohár
2024-09-29 21:47   ` Steve French
2024-09-29 21:58     ` Pali Rohár
2024-09-29 18:50 ` [PATCH 6/7] cifs: Validate content of native symlink Pali Rohár
2024-09-29 21:48   ` Steve French
2024-09-29 22:19     ` Pali Rohár
2024-09-30 20:00       ` Pali Rohár
2024-09-29 18:50 ` [PATCH 7/7] cifs: Fix creating and resolving absolute NT-style symlinks Pali Rohár
2024-09-29 22:03 ` [PATCH 0/7] cifs: Improve support for native SMB symlinks Steve French
2024-10-05 14:07   ` Pali Rohár
2024-10-05 14:02 ` [PATCH v2 0/6] " Pali Rohár
2024-10-05 14:02   ` [PATCH v2 1/6] cifs: Improve creating native symlinks pointing to directory Pali Rohár
2024-10-05 14:02   ` [PATCH v2 2/6] cifs: Fix creating native symlinks pointing to current or parent directory Pali Rohár
2024-10-05 14:02   ` [PATCH v2 3/6] cifs: Fix parsing native symlinks relative to the export Pali Rohár
2024-10-05 14:02   ` Pali Rohár [this message]
2024-10-13 17:56     ` [PATCH v2 4/6] cifs: Fix parsing native symlinks directory/file type Steve French
2024-10-14  9:52       ` Pali Rohár
2024-10-15 14:30     ` [PATCH v3] " Pali Rohár
2024-12-09 18:02       ` Pali Rohár
2024-10-05 14:02   ` [PATCH v2 5/6] cifs: Validate content of native symlink Pali Rohár
2024-10-13  4:21     ` Steve French
2024-10-14  9:39       ` Pali Rohár
2024-10-05 14:03   ` [PATCH v2 6/6] cifs: Fix creating and resolving absolute NT-style symlinks Pali Rohár
2024-10-07  3:59     ` Steve French
2024-10-07 18:09       ` Pali Rohár
2024-12-09 17:58     ` Pali Rohár

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241005140300.19416-5-pali@kernel.org \
    --to=pali@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pc@manguebit.com \
    --cc=ronniesahlberg@gmail.com \
    --cc=sfrench@samba.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox