mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] ksmbd: return the cause of a failed link
@ 2026-11-28 15:18 Ian Bridges
  0 siblings, 0 replies; only message in thread
From: Ian Bridges @ 2026-11-28 15:18 UTC (permalink / raw)
  To: linkinjeon
  Cc: senozhatsky, tom, chenxiaosong, linux-cifs, linux-kernel, Ian Bridges

smb2_create_link() turns every error of the link, and of the removal of
a target that the link replaces, into -EINVAL. The client gets
STATUS_INVALID_PARAMETER whatever the cause.

Add smb_errno_to_ntstatus(), a default mapping from errno values to NT
status codes. Return the errors of the link path as they are and map
them with it in smb2_set_info().

A directory as the source now returns -EISDIR. vfs_link() returns
-EPERM for a filesystem without hard links. Check for that case in
ksmbd_vfs_link() before the target lookup and return -EOPNOTSUPP.

The lookup of the link name returns -EXDEV for a name that leaves the
share. Turn it into -EACCES so that it is still reported as access
denied and not as a different device.

Signed-off-by: Ian Bridges <icb@fastmail.org>
---
Changes in v2:
- v2 maps the errors with a new default mapping in smb_common.c instead
of the existing mapping in smb2_set_info(). Later patches will convert
the other handlers to this mapping, one handler per patch.
- A filesystem without hard links now gives STATUS_NOT_SUPPORTED.
- A source at the link limit gives STATUS_TOO_MANY_LINKS.
- A directory source fails before the link name is parsed.
ksmbd_vfs_link() checks for hard link support before its own lookup
of the target.

Tested on ksmbd-for-next 4543a3349127 with KASAN. The patch builds with
W=1 and sparse and adds no smatch warning. A script tried one link for
each cause of failure. A link into a directory without write access and
a link of an immutable file now return STATUS_ACCESS_DENIED. A link on
vfat and a link from vfat into ext4 return STATUS_NOT_SUPPORTED. A link
from ext4 into vfat returns STATUS_NOT_SAME_DEVICE. A source with 65000
links on ext4 returns STATUS_TOO_MANY_LINKS. A directory as the source
returns STATUS_FILE_IS_A_DIRECTORY. Replacing a directory that is not
empty returns STATUS_DIRECTORY_NOT_EMPTY. Before this patch, all of
these returned STATUS_INVALID_PARAMETER. A name that leaves the share
still returns STATUS_ACCESS_DENIED. A normal link and a link that
replaces a file still succeed. smbtorture gives the same results as on
the base.

v1: https://lore.kernel.org/linux-cifs/20261128013952.1691873-1-icb@fastmail.org/

This patch was developed with LLM assistance.

 fs/smb/server/smb2pdu.c    | 21 ++++++++++++--------
 fs/smb/server/smb_common.c | 40 ++++++++++++++++++++++++++++++++++++++
 fs/smb/server/smb_common.h |  1 +
 fs/smb/server/vfs.c        |  9 +++++++++
 4 files changed, 63 insertions(+), 8 deletions(-)

diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 38326db8da40..ab75104e5aa6 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -8924,6 +8924,9 @@ static int smb2_create_link(struct ksmbd_work *work,
 			le32_to_cpu(file_info->FileNameLength))
 		return -EINVAL;
 
+	if (S_ISDIR(file_inode(filp)->i_mode))
+		return -EISDIR;
+
 	ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n");
 	pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP);
 	if (!pathname)
@@ -8932,7 +8935,7 @@ static int smb2_create_link(struct ksmbd_work *work,
 	link_name = smb2_get_name(file_info->FileName,
 				  le32_to_cpu(file_info->FileNameLength),
 				  local_nls);
-	if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) {
+	if (IS_ERR(link_name)) {
 		rc = -EINVAL;
 		goto out;
 	}
@@ -8948,16 +8951,17 @@ static int smb2_create_link(struct ksmbd_work *work,
 	rc = ksmbd_vfs_kern_path_start_removing(work, link_name, LOOKUP_NO_SYMLINKS,
 						&path, 0);
 	if (rc) {
+		/* -EXDEV here means the name leaves the share */
+		if (rc == -EXDEV)
+			rc = -EACCES;
 		if (rc != -ENOENT)
 			goto out;
 	} else {
 		if (file_info->ReplaceIfExists) {
 			rc = ksmbd_vfs_remove_file(work, &path);
-			if (rc) {
-				rc = -EINVAL;
+			if (rc)
 				ksmbd_debug(SMB, "cannot delete %s\n",
 					    link_name);
-			}
 		} else {
 			rc = -EEXIST;
 			ksmbd_debug(SMB, "link already exists\n");
@@ -8967,8 +8971,6 @@ static int smb2_create_link(struct ksmbd_work *work,
 			goto out;
 	}
 	rc = ksmbd_vfs_link(work, target_name, link_name);
-	if (rc)
-		rc = -EINVAL;
 out:
 
 	if (!IS_ERR(link_name))
@@ -9442,7 +9444,7 @@ int smb2_set_info(struct ksmbd_work *work)
 	struct smb2_set_info_rsp *rsp;
 	struct ksmbd_file *fp = NULL;
 	int rc = 0;
-	bool chseq_err = false;
+	bool chseq_err = false, map_err = false;
 	unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
 
 	ksmbd_debug(SMB, "Received smb2 set info request\n");
@@ -9494,6 +9496,7 @@ int smb2_set_info(struct ksmbd_work *work)
 	case SMB2_O_INFO_FILE:
 		ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
 		rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf);
+		map_err = req->FileInfoClass == FILE_LINK_INFORMATION;
 		break;
 	case SMB2_O_INFO_SECURITY:
 		ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
@@ -9519,7 +9522,9 @@ int smb2_set_info(struct ksmbd_work *work)
 	return 0;
 
 err_out:
-	if (rc == -EACCES || rc == -EPERM || rc == -EXDEV) {
+	if (map_err && rc != -EMSGSIZE) {
+		rsp->hdr.Status = smb_errno_to_ntstatus(rc);
+	} else if (rc == -EACCES || rc == -EPERM || rc == -EXDEV) {
 		if (fp && req->InfoType == SMB2_O_INFO_FILE &&
 		    req->FileInfoClass == FILE_DISPOSITION_INFORMATION &&
 		    fp->f_ci->m_fattr & FILE_ATTRIBUTE_READONLY_LE)
diff --git a/fs/smb/server/smb_common.c b/fs/smb/server/smb_common.c
index 6595d40b06bc..4996840bc9d1 100644
--- a/fs/smb/server/smb_common.c
+++ b/fs/smb/server/smb_common.c
@@ -909,3 +909,43 @@ __le32 smb_map_generic_desired_access(__le32 daccess)
 
 	return daccess;
 }
+
+/* Map a negative errno to a default NT status. */
+__le32 smb_errno_to_ntstatus(int err)
+{
+	switch (err) {
+	case -EACCES:
+	case -EPERM:
+		return STATUS_ACCESS_DENIED;
+	case -ENOENT:
+		return STATUS_OBJECT_NAME_NOT_FOUND;
+	case -ENOTDIR:
+		return STATUS_OBJECT_PATH_NOT_FOUND;
+	case -EEXIST:
+		return STATUS_OBJECT_NAME_COLLISION;
+	case -EISDIR:
+		return STATUS_FILE_IS_A_DIRECTORY;
+	case -ENOTEMPTY:
+		return STATUS_DIRECTORY_NOT_EMPTY;
+	case -ENAMETOOLONG:
+		return STATUS_OBJECT_NAME_INVALID;
+	case -EXDEV:
+		return STATUS_NOT_SAME_DEVICE;
+	case -EMLINK:
+		return STATUS_TOO_MANY_LINKS;
+	case -ENOSPC:
+		return STATUS_DISK_FULL;
+	case -EDQUOT:
+		return STATUS_DISK_QUOTA_EXCEEDED;
+	case -EROFS:
+		return STATUS_MEDIA_WRITE_PROTECTED;
+	case -EOPNOTSUPP:
+		return STATUS_NOT_SUPPORTED;
+	case -ENOMEM:
+		return STATUS_INSUFFICIENT_RESOURCES;
+	case -EINVAL:
+		return STATUS_INVALID_PARAMETER;
+	default:
+		return STATUS_UNEXPECTED_IO_ERROR;
+	}
+}
diff --git a/fs/smb/server/smb_common.h b/fs/smb/server/smb_common.h
index d3d19f1a23ea..1d93bd00fcd1 100644
--- a/fs/smb/server/smb_common.h
+++ b/fs/smb/server/smb_common.h
@@ -300,6 +300,7 @@ unsigned int ksmbd_server_side_copy_max_chunk_size(void);
 unsigned int ksmbd_server_side_copy_max_total_size(void);
 bool is_asterisk(char *p);
 __le32 smb_map_generic_desired_access(__le32 daccess);
+__le32 smb_errno_to_ntstatus(int err);
 
 /*
  * Get the body of the smb message excluding the 4 byte rfc1002 headers
diff --git a/fs/smb/server/vfs.c b/fs/smb/server/vfs.c
index 5b0722fe7788..2a48b875c327 100644
--- a/fs/smb/server/vfs.c
+++ b/fs/smb/server/vfs.c
@@ -705,6 +705,15 @@ int ksmbd_vfs_link(struct ksmbd_work *work, const char *oldname,
 		goto out1;
 	}
 
+	/*
+	 * Fail first if the source filesystem has no hard links.
+	 * vfs_link() would only see this on the target directory, and it
+	 * returns -EPERM for it.
+	 */
+	err = -EOPNOTSUPP;
+	if (!d_inode(oldpath.dentry->d_sb->s_root)->i_op->link)
+		goto out2;
+
 	dentry = ksmbd_vfs_kern_path_create(work, newname,
 					    LOOKUP_NO_SYMLINKS | LOOKUP_REVAL,
 					    &newpath);

base-commit: 4543a3349127baa8395397b7f4aa930d9a4f8dd5
-- 
2.47.3


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-28 15:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-28 15:18 [PATCH v2] ksmbd: return the cause of a failed link Ian Bridges

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®