All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scsi: reject negative SG_SET_TIMEOUT values
@ 2026-10-17  8:45 Wang Yingjie
  2026-10-19 21:34 ` Bart Van Assche
  0 siblings, 1 reply; 8+ messages in thread
From: Wang Yingjie @ 2026-10-17  8:45 UTC (permalink / raw)
  To: mkp@kernel.org, axboe@kernel.dk
  Cc: fujita.tomonori@lab.ntt.co.jp,
	James.Bottomley@HansenPartnership.com, linux-scsi@vger.kernel.org,
	linux-block@vger.kernel.org, linux-kernel@vger.kernel.org

From: Yingjie Wang <x-professor@outlook.com>

SG_SET_TIMEOUT reads its argument as a signed int in USER_HZ units. The
sg character device rejects negative values, while the block SCSI ioctl
and BSG paths pass them to clock_t_to_jiffies(), which takes an unsigned
long.

On a 64-bit system with HZ=1000, passing -5 first converts it to a large
unsigned value, causing clock_t_to_jiffies() to return ULONG_MAX.
Storing that result in sdev->sg_timeout or bd->timeout truncates it to
UINT_MAX. SG_GET_TIMEOUT then reports 429496729 ticks, and an SG_IO
request with a zero per-request timeout inherits a timeout of about
49.7 days.

Reject negative values in both paths with -EIO, matching the
longstanding behavior of the sg character device.

The block SCSI path predates Git history. The BSG path has exposed the
same behavior since BSG was introduced.

Fixes: 3d6392cfbd7d ("bsg: support for full generic block layer SG v3")
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
 block/bsg.c               | 2 ++
 drivers/scsi/scsi_ioctl.c | 9 ++++++---
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/block/bsg.c b/block/bsg.c
index 82aaf3cee582..681da79a5ba2 100644
--- a/block/bsg.c
+++ b/block/bsg.c
@@ -132,6 +132,8 @@ static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	case SG_SET_TIMEOUT:
 		if (get_user(val, intp))
 			return -EFAULT;
+		if (val < 0)
+			return -EIO;
 		bd->timeout = clock_t_to_jiffies(val);
 		return 0;
 	case SG_GET_TIMEOUT:
diff --git a/drivers/scsi/scsi_ioctl.c b/drivers/scsi/scsi_ioctl.c
index c14f81403a09..162a21112d49 100644
--- a/drivers/scsi/scsi_ioctl.c
+++ b/drivers/scsi/scsi_ioctl.c
@@ -195,10 +195,13 @@ static int sg_set_timeout(struct scsi_device *sdev, int __user *p)
 {
 	int timeout, err = get_user(timeout, p);
 
-	if (!err)
-		sdev->sg_timeout = clock_t_to_jiffies(timeout);
+	if (err)
+		return err;
+	if (timeout < 0)
+		return -EIO;
 
-	return err;
+	sdev->sg_timeout = clock_t_to_jiffies(timeout);
+	return 0;
 }
 
 static int sg_get_reserved_size(struct scsi_device *sdev, int __user *p)

base-commit: 58161ead29e50d70a317ea26b472e0c7508e69da
-- 
2.43.0


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

end of thread, other threads:[~2026-10-29  2:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-17  8:45 [PATCH] scsi: reject negative SG_SET_TIMEOUT values Wang Yingjie
2026-10-19 21:34 ` Bart Van Assche
2026-10-21 18:18   ` [PATCH v2] scsi: handle out-of-range " Wang Yingjie
2026-10-21 20:29     ` Bart Van Assche
2026-10-22  2:55       ` [PATCH v3] " Wang Yingjie
2026-10-22  3:24         ` Bart Van Assche
2026-10-24  3:04         ` Martin K. Petersen
2026-10-29  2:03         ` Martin K. Petersen (Oracle)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.