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
  2026-11-29  2:03 ` Namjae Jeon
  0 siblings, 1 reply; 3+ messages 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] 3+ messages in thread

* Re: [PATCH v2] ksmbd: return the cause of a failed link
  2026-11-28 15:18 [PATCH v2] ksmbd: return the cause of a failed link Ian Bridges
@ 2026-11-29  2:03 ` Namjae Jeon
  2026-11-29 14:42   ` Ian Bridges
  0 siblings, 1 reply; 3+ messages in thread
From: Namjae Jeon @ 2026-11-29  2:03 UTC (permalink / raw)
  To: Ian Bridges; +Cc: senozhatsky, tom, chenxiaosong, linux-cifs, linux-kernel

> @@ -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)
Do you have a plan to convert the other request handlers to
smb_errno_to_ntstatus() ?

> +{
> +       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:
Please handle -ENOTSUPP here as well.

> +               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;
FUSE registers ->link even when the daemon lacks hard-link support. If
the daemon returns -ENOSYS for FUSE_LINK, fuse_link() returns -EPERM
and ksmbd reports STATUS_ACCESS_DENIED. This may also be a layering
violation.

> +
>         dentry = ksmbd_vfs_kern_path_create(work, newname,
>                                             LOOKUP_NO_SYMLINKS | LOOKUP_REVAL,
>                                             &newpath);
This lookup also uses LOOKUP_BENEATH. If it rejects a path with
-EXDEV, the new mapping returns STATUS_NOT_SAME_DEVICE. Please
translate -EXDEV to -EACCES in the IS_ERR(dentry) branch so this
case returns STATUS_ACCESS_DENIED.
Thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] ksmbd: return the cause of a failed link
  2026-11-29  2:03 ` Namjae Jeon
@ 2026-11-29 14:42   ` Ian Bridges
  0 siblings, 0 replies; 3+ messages in thread
From: Ian Bridges @ 2026-11-29 14:42 UTC (permalink / raw)
  To: linkinjeon
  Cc: senozhatsky, tom, chenxiaosong, linux-cifs, linux-kernel, Ian Bridges

Hi Namjae,

Thanks for the review. Please drop this patch. I'm moving the work to
my backlog, for two reasons.

The patch does not fix a bug. A link that should fail already fails,
and only the status is less exact than it could be. And your comments
showed me that a correct change is a bigger job than I expected.

The core problem is that one errno often has more than one meaning by
the time it reaches the handler. Your -EXDEV comment is one case. The
lookup returns it for a name that leaves the share, and the link code
returns it for a different mount. The FUSE case is another. vfs_link()
returns -EPERM for an immutable file and also for a filesystem that
refuses hard links, and the i_op check only finds some of the second
kind. A shared map in the handler cannot tell these apart. Each one
needs a special case next to the map, and v2 was already collecting
them.

I tried a version that solves this at the source. Each helper in vfs.c
returns errnos that have one meaning each. Where the VFS itself gives
one errno for two causes, the helper looks at the state after the
failure to find the cause, without reading i_op. Results that are
ksmbd's own decisions set the status where they are decided. Each
handler then ends with the shared map and no special cases.

That gave the right statuses for link in my tests. But it does not
work as a small change. The lookup helpers are shared, so cleaning up
their errnos changes the statuses of smb2_open() and other handlers at
the same time. Some of those handlers also depend on the old values.
To be correct, every handler has to be converted and tested together.
That is a series of its own, and it should not ride on a patch that
fixes no bug.

I'll come back to it once the fixes I have queued are out.

Thanks,
Ian

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-29 14:42 UTC | newest]

Thread overview: 3+ messages (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
2026-11-29  2:03 ` Namjae Jeon
2026-11-29 14:42   ` 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®