mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Wei Huang <wei.huang2@amd.com>
To: <dmaengine@vger.kernel.org>
Cc: <vkoul@kernel.org>, <Frank.Li@kernel.org>, <shuah@kernel.org>,
	<nathan.lynch@amd.com>, <wei.huang2@amd.com>,
	<mario.limonciello@amd.com>, <Stephen.Bates@amd.com>,
	<tycho@kernel.org>, <suravee.suthikulpanit@amd.com>,
	<rientjes@google.com>, <John.Kariuki@amd.com>, <jic23@kernel.org>,
	<weixugc@google.com>, <bhelgaas@google.com>,
	<Shyam.Iyer@dell.com>, <linux-kernel@vger.kernel.org>,
	<linux-kselftest@vger.kernel.org>, <linux-pci@vger.kernel.org>
Subject: [PATCH v1 19/44] dmaengine: sdxi: validate and copy key ioctl payloads
Date: Thu, 19 Nov 2026 14:22:55 -0600	[thread overview]
Message-ID: <20261119202321.1734355-20-wei.huang2@amd.com> (raw)
In-Reply-To: <20261119202321.1734355-1-wei.huang2@amd.com>

Size variable-length key bitmap and entry payloads from their fixed
UAPI headers, validate table bounds before allocation, and copy only
the bytes described by the shared AKey/RKey ABI. Key handlers, which
will be introduced later, remain unsupported in this patch.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/ioctl.c | 161 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 150 insertions(+), 11 deletions(-)

diff --git a/drivers/dma/sdxi/ioctl.c b/drivers/dma/sdxi/ioctl.c
index 70dcde916c82..682b0b074f38 100644
--- a/drivers/dma/sdxi/ioctl.c
+++ b/drivers/dma/sdxi/ioctl.c
@@ -615,17 +615,85 @@ static bool sdxi_ioctl_has_trailing_payload(unsigned int cmd)
 	       cmd == SDXI_GET_AKEYS || cmd == SDXI_SET_AKEYS;
 }
 
+static bool sdxi_key_list_cmd(unsigned int cmd)
+{
+	return cmd == SDXI_GET_RKEY_LIST || cmd == SDXI_GET_AKEY_LIST;
+}
+
+static bool sdxi_key_entries_cmd(unsigned int cmd)
+{
+	return cmd == SDXI_GET_RKEYS || cmd == SDXI_SET_RKEYS ||
+	       cmd == SDXI_GET_AKEYS || cmd == SDXI_SET_AKEYS;
+}
+
+static u32 sdxi_key_table_entries(unsigned int cmd, struct sdxi_file *sf)
+{
+	if (cmd == SDXI_GET_RKEY_LIST || cmd == SDXI_GET_RKEYS ||
+	    cmd == SDXI_SET_RKEYS)
+		return sf->process->sdxi->rkey_entries;
+
+	if (!sf->cxt || !sf->cxt->akey_table)
+		return 0;
+	return ARRAY_SIZE(sf->cxt->akey_table->entry);
+}
+
+static int sdxi_key_ioctl_sizes(unsigned int cmd, struct sdxi_file *sf,
+				const void *header, size_t header_size,
+				size_t *copyin_size, size_t *copyout_size,
+				size_t *allocation_size)
+{
+	size_t payload_size;
+	u32 nentries;
+	u32 nkeys;
+
+	nentries = sdxi_key_table_entries(cmd, sf);
+
+	if (sdxi_key_list_cmd(cmd)) {
+		const struct sdxi_key_list_args *args = header;
+		size_t required = DIV_ROUND_UP(nentries, 8);
+
+		*copyin_size = header_size;
+		*copyout_size = header_size + min_t(size_t, args->buf_size,
+						    required);
+		*allocation_size = *copyout_size;
+		return 0;
+	}
+
+	if (cmd == SDXI_GET_RKEYS || cmd == SDXI_GET_AKEYS)
+		nkeys = ((const struct sdxi_get_keys_args *)header)->nkeys;
+	else
+		nkeys = ((const struct sdxi_set_keys_args *)header)->nkeys;
+
+	if (nkeys > min_t(u32, nentries, 4096))
+		return -EINVAL;
+	if (check_mul_overflow((size_t)nkeys, sizeof(struct sdxi_key_entry),
+			       &payload_size) ||
+	    check_add_overflow(header_size, payload_size, allocation_size))
+		return -EOVERFLOW;
+
+	*copyin_size = *allocation_size;
+	if (cmd == SDXI_GET_RKEYS || cmd == SDXI_GET_AKEYS)
+		*copyout_size = *allocation_size;
+	else
+		*copyout_size = header_size;
+	return 0;
+}
+
 static long sdxi_ioctl_live(struct file *file, unsigned int cmd,
 			    unsigned long arg)
 {
 	const struct sdxi_ioctl_desc *desc;
 	struct sdxi_file *sf = file->private_data;
+	size_t copyout_size;
+	size_t copyin_size;
 	unsigned int canonical_size;
 	unsigned int user_size;
-	unsigned int size;
+	size_t size;
 	unsigned int nr;
 	char stack_data[128];
 	void *data = stack_data;
+	bool header_copied = false;
+	bool variable;
 	bool allowed;
 	int ret;
 
@@ -666,32 +734,69 @@ static long sdxi_ioctl_live(struct file *file, unsigned int cmd,
 
 	canonical_size = _IOC_SIZE(desc->cmd);
 	user_size = _IOC_SIZE(cmd);
-	size = max(canonical_size, user_size);
-	if (size > PAGE_SIZE)
+	copyin_size = (_IOC_DIR(cmd) & _IOC_WRITE) ? user_size : 0;
+	copyout_size = (_IOC_DIR(cmd) & _IOC_READ) ? user_size : 0;
+	size = max_t(size_t, canonical_size, user_size);
+
+	variable = sdxi_key_list_cmd(desc->cmd) ||
+		   sdxi_key_entries_cmd(desc->cmd);
+	if (variable) {
+		if (user_size < canonical_size)
+			return -EINVAL;
+		if (canonical_size > sizeof(stack_data))
+			return -E2BIG;
+		if (copy_from_user(stack_data, (void __user *)arg,
+				   canonical_size))
+			return -EFAULT;
+		header_copied = true;
+		ret = sdxi_key_ioctl_sizes(desc->cmd, sf, stack_data,
+					   canonical_size, &copyin_size,
+					   &copyout_size, &size);
+		if (ret)
+			return ret;
+	}
+
+	if (size > SZ_1M)
 		return -E2BIG;
 
 	if (size > sizeof(stack_data)) {
 		data = kzalloc(size, GFP_KERNEL);
 		if (!data)
 			return -ENOMEM;
-	} else {
+	} else if (!header_copied) {
 		memset(data, 0, size);
+	} else if (size > canonical_size) {
+		memset(data + canonical_size, 0, size - canonical_size);
 	}
 
-	if ((_IOC_DIR(cmd) & _IOC_WRITE) &&
-	    copy_from_user(data, (void __user *)arg, user_size)) {
-		ret = -EFAULT;
-		goto out;
+	if (_IOC_DIR(cmd) & _IOC_WRITE) {
+		if (header_copied) {
+			if (data != stack_data)
+				memcpy(data, stack_data, canonical_size);
+			if (copyin_size > canonical_size &&
+			    copy_from_user(data + canonical_size,
+					   (void __user *)(arg + canonical_size),
+					   copyin_size - canonical_size)) {
+				ret = -EFAULT;
+				goto out;
+			}
+		} else if (copy_from_user(data, (void __user *)arg,
+					  copyin_size)) {
+			ret = -EFAULT;
+			goto out;
+		}
 	}
 
 	ret = desc->func(file, sf, data);
 
+	if (ret == -ENOSPC && sdxi_key_list_cmd(desc->cmd))
+		copyout_size = canonical_size;
 	if ((_IOC_DIR(cmd) & _IOC_READ) && user_size > canonical_size &&
 	    !sdxi_ioctl_has_trailing_payload(desc->cmd))
 		memset((u8 *)data + canonical_size, 0,
 		       user_size - canonical_size);
 	if ((_IOC_DIR(cmd) & _IOC_READ) &&
-	    copy_to_user((void __user *)arg, data, user_size))
+	    copy_to_user((void __user *)arg, data, copyout_size))
 		ret = -EFAULT;
 
 out:
@@ -716,7 +821,7 @@ static long sdxi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	return ret;
 }
 
-static int sdxi_mmap(struct file *file, struct vm_area_struct *vma)
+static int sdxi_mmap_live(struct file *file, struct vm_area_struct *vma)
 {
 	struct sdxi_file *sf = file->private_data;
 	struct sdxi_cxt *cxt;
@@ -781,7 +886,24 @@ static int sdxi_mmap(struct file *file, struct vm_area_struct *vma)
 	return ret;
 }
 
-static __poll_t sdxi_cxt_poll(struct file *file, poll_table *wait)
+static int sdxi_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct sdxi_file *sf = file->private_data;
+	struct sdxi_dev *sdxi;
+	int ret;
+
+	if (!sf || !sf->process)
+		return -EINVAL;
+	sdxi = sf->process->sdxi;
+
+	if (!down_read_trylock(&sdxi->state_lock))
+		return -EAGAIN;
+	ret = sdxi->online ? sdxi_mmap_live(file, vma) : -ENODEV;
+	up_read(&sdxi->state_lock);
+	return ret;
+}
+
+static __poll_t sdxi_cxt_poll_live(struct file *file, poll_table *wait)
 {
 	struct sdxi_file *sf = file->private_data;
 	struct sdxi_cxt *cxt;
@@ -805,6 +927,23 @@ static __poll_t sdxi_cxt_poll(struct file *file, poll_table *wait)
 	return mask;
 }
 
+static __poll_t sdxi_cxt_poll(struct file *file, poll_table *wait)
+{
+	struct sdxi_file *sf = file->private_data;
+	struct sdxi_dev *sdxi;
+	__poll_t mask;
+
+	if (!sf || !sf->process)
+		return EPOLLERR | EPOLLHUP;
+	sdxi = sf->process->sdxi;
+
+	down_read(&sdxi->state_lock);
+	mask = sdxi->online ? sdxi_cxt_poll_live(file, wait) :
+			      EPOLLERR | EPOLLHUP;
+	up_read(&sdxi->state_lock);
+	return mask;
+}
+
 static int sdxi_open(struct inode *inode, struct file *file)
 {
 	struct sdxi_dev *sdxi;
-- 
2.56.1


  parent reply	other threads:[~2026-11-19 20:27 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
2026-11-19 20:22 ` [PATCH v1 01/44] dmaengine: sdxi: cache hardware version for feature gating Wei Huang
2026-11-19 20:22 ` [PATCH v1 02/44] dmaengine: sdxi: define key and error MMIO registers Wei Huang
2026-11-19 20:22 ` [PATCH v1 03/44] dmaengine: sdxi: define SDXI hardware layouts Wei Huang
2026-11-19 20:22 ` [PATCH v1 04/44] dmaengine: sdxi: encode admin descriptors Wei Huang
2026-11-19 20:22 ` [PATCH v1 05/44] dmaengine: sdxi: support timeout-aware completion waits Wei Huang
2026-11-19 20:22 ` [PATCH v1 06/44] dmaengine: sdxi: support the full context table hierarchy Wei Huang
2026-11-19 20:22 ` [PATCH v1 07/44] dmaengine: sdxi: implement synchronous admin operations Wei Huang
2026-11-19 20:22 ` [PATCH v1 08/44] dmaengine: sdxi: add core RKey table management Wei Huang
2026-11-19 20:22 ` [PATCH v1 09/44] dmaengine: sdxi: handle hardware error logs Wei Huang
2026-11-19 20:22 ` [PATCH v1 10/44] dmaengine: sdxi: separate context and submission resources Wei Huang
2026-11-19 20:22 ` [PATCH v1 11/44] dmaengine: sdxi: support userspace context lifetimes Wei Huang
2026-11-19 20:22 ` [PATCH v1 12/44] dmaengine: sdxi: route context interrupts to eventfd Wei Huang
2026-11-19 20:22 ` [PATCH v1 13/44] dmaengine: sdxi: kill user contexts on fatal errors Wei Huang
2026-11-19 20:22 ` [PATCH v1 14/44] dmaengine: sdxi: define the ioctl UAPI surface Wei Huang
2026-11-19 20:22 ` [PATCH v1 15/44] dmaengine: sdxi: add internal ioctl definitions Wei Huang
2026-11-19 20:22 ` [PATCH v1 16/44] dmaengine: sdxi: register core character devices Wei Huang
2026-11-19 20:22 ` [PATCH v1 17/44] dmaengine: sdxi: implement fixed-size ioctl dispatch Wei Huang
2026-11-19 20:22 ` [PATCH v1 18/44] dmaengine: sdxi: implement userspace context file operations Wei Huang
2026-11-19 20:22 ` Wei Huang [this message]
2026-11-19 20:22 ` [PATCH v1 20/44] dmaengine: sdxi: implement RKey management ioctls Wei Huang
2026-11-19 20:22 ` [PATCH v1 21/44] dmaengine: sdxi: implement AKey " Wei Huang
2026-11-19 20:22 ` [PATCH v1 22/44] dmaengine: sdxi: expose operation-group discovery Wei Huang
2026-11-19 20:22 ` [PATCH v1 23/44] dmaengine: sdxi: implement typed vendor admin transport Wei Huang
2026-11-19 20:23 ` [PATCH v1 24/44] dmaengine: sdxi: expose character device attributes Wei Huang
2026-11-19 20:23 ` [PATCH v1 25/44] dmaengine: sdxi: add PF and VF device naming Wei Huang
2026-11-19 20:23 ` [PATCH v1 26/44] dmaengine: sdxi: add SDXI trace events Wei Huang
2026-11-19 20:23 ` [PATCH v1 27/44] dmaengine: sdxi: support suspend and resume Wei Huang
2026-11-19 20:23 ` [PATCH v1 28/44] dmaengine: sdxi: amd: define QoS request formats Wei Huang
2026-11-19 20:23 ` [PATCH v1 29/44] dmaengine: sdxi: amd: add QoS validation scaffolding Wei Huang
2026-11-19 20:23 ` [PATCH v1 30/44] dmaengine: sdxi: amd: build QoS admin descriptors Wei Huang
2026-11-19 20:23 ` [PATCH v1 31/44] dmaengine: sdxi: amd: prevalidate QoS operations Wei Huang
2026-11-19 20:23 ` [PATCH v1 32/44] dmaengine: sdxi: amd: execute QoS vendor operations Wei Huang
2026-11-19 20:23 ` [PATCH v1 33/44] dmaengine: sdxi: amd: register QoS vendor handler Wei Huang
2026-11-19 20:23 ` [PATCH v1 34/44] dmaengine: sdxi: amd: test QoS validation with KUnit Wei Huang
2026-11-19 20:23 ` [PATCH v1 35/44] selftests: sdxi: add build scaffold Wei Huang
2026-11-19 20:23 ` [PATCH v1 36/44] selftests: sdxi: add shared descriptor and context helpers Wei Huang
2026-11-19 20:23 ` [PATCH v1 37/44] selftests: sdxi: test context creation Wei Huang
2026-11-19 20:23 ` [PATCH v1 38/44] selftests: sdxi: test NOP descriptor submission Wei Huang
2026-11-19 20:23 ` [PATCH v1 39/44] selftests: sdxi: test copy submission and ring wrap Wei Huang
2026-11-19 20:23 ` [PATCH v1 40/44] selftests: sdxi: exercise dataplane error paths Wei Huang
2026-11-19 20:23 ` [PATCH v1 41/44] selftests: sdxi: test device ioctls Wei Huang
2026-11-19 20:23 ` [PATCH v1 42/44] selftests: sdxi: test vendor admin ioctls Wei Huang
2026-11-19 20:23 ` [PATCH v1 43/44] selftests: sdxi: test RKey ioctls Wei Huang
2026-11-19 20:23 ` [PATCH v1 44/44] MAINTAINERS: cover SDXI UAPI, vendor files, and selftests Wei Huang

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=20261119202321.1734355-20-wei.huang2@amd.com \
    --to=wei.huang2@amd.com \
    --cc=Frank.Li@kernel.org \
    --cc=John.Kariuki@amd.com \
    --cc=Shyam.Iyer@dell.com \
    --cc=Stephen.Bates@amd.com \
    --cc=bhelgaas@google.com \
    --cc=dmaengine@vger.kernel.org \
    --cc=jic23@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=mario.limonciello@amd.com \
    --cc=nathan.lynch@amd.com \
    --cc=rientjes@google.com \
    --cc=shuah@kernel.org \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=tycho@kernel.org \
    --cc=vkoul@kernel.org \
    --cc=weixugc@google.com \
    /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

all inboxes | Powered by JetHome®