mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chaitanya Mishra <chaitanyamishra.ai@gmail.com>
To: Eddie James <eajames@linux.ibm.com>
Cc: Ninad Palsule <ninad@linux.ibm.com>,
	linux-fsi@lists.ozlabs.org, linux-kernel@vger.kernel.org,
	Chaitanya Mishra <chaitanyamishra.ai@gmail.com>
Subject: [PATCH v1 1/1] fsi: cfam: clamp chunk length to remaining count
Date: Fri, 16 Jan 2026 09:31:40 +0530	[thread overview]
Message-ID: <20260116040140.65394-2-chaitanyamishra.ai@gmail.com> (raw)
In-Reply-To: <20260116040140.65394-1-chaitanyamishra.ai@gmail.com>

cfam_{read,write} split requests into up to 4-byte aligned chunks.
The per-iteration length is computed from the full count and the
current alignment. Once total_len advances, this can exceed the
remaining bytes, leading to copy_{to,from}_user() touching bytes past
the user buffer and advancing the file offset too far.

Clamp each chunk to the minimum of the alignment-based length and the
remaining bytes so each iteration handles only the bytes left.

Fixes: d1dcd6782576 ("fsi: Add cfam char devices")
Signed-off-by: Chaitanya Mishra <chaitanyamishra.ai@gmail.com>
---
 drivers/fsi/fsi-core.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/fsi/fsi-core.c b/drivers/fsi/fsi-core.c
index c6c115993ebc..8a2a02df52ac 100644
--- a/drivers/fsi/fsi-core.c
+++ b/drivers/fsi/fsi-core.c
@@ -674,8 +674,7 @@ static ssize_t cfam_read(struct file *filep, char __user *buf, size_t count,
 	for (total_len = 0; total_len < count; total_len += read_len) {
 		__be32 data;
 
-		read_len = min_t(size_t, count, 4);
-		read_len -= off & 0x3;
+		read_len = min_t(size_t, 4 - (off & 0x3), count - total_len);
 
 		rc = fsi_slave_read(slave, off, &data, read_len);
 		if (rc)
@@ -711,8 +710,7 @@ static ssize_t cfam_write(struct file *filep, const char __user *buf,
 	for (total_len = 0; total_len < count; total_len += write_len) {
 		__be32 data;
 
-		write_len = min_t(size_t, count, 4);
-		write_len -= off & 0x3;
+		write_len = min_t(size_t, 4 - (off & 0x3), count - total_len);
 
 		rc = copy_from_user(&data, buf + total_len, write_len);
 		if (rc) {
-- 
2.50.1 (Apple Git-155)


      reply	other threads:[~2026-01-16  4:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-16  4:01 [PATCH v1 0/1] " Chaitanya Mishra
2026-01-16  4:01 ` Chaitanya Mishra [this message]

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=20260116040140.65394-2-chaitanyamishra.ai@gmail.com \
    --to=chaitanyamishra.ai@gmail.com \
    --cc=eajames@linux.ibm.com \
    --cc=linux-fsi@lists.ozlabs.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ninad@linux.ibm.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®