From: Wang Yingjie <x-professor@outlook.com>
To: "mkp@kernel.org" <mkp@kernel.org>, "axboe@kernel.dk" <axboe@kernel.dk>
Cc: "fujita.tomonori@lab.ntt.co.jp" <fujita.tomonori@lab.ntt.co.jp>,
"James.Bottomley@HansenPartnership.com"
<James.Bottomley@HansenPartnership.com>,
"linux-scsi@vger.kernel.org" <linux-scsi@vger.kernel.org>,
"linux-block@vger.kernel.org" <linux-block@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: [PATCH] scsi: reject negative SG_SET_TIMEOUT values
Date: Sat, 17 Oct 2026 08:45:59 +0000 [thread overview]
Message-ID: <20261017084547.2880694-1-x-professor@outlook.com> (raw)
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
next reply other threads:[~2026-10-17 8:46 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-17 8:45 Wang Yingjie [this message]
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
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=20261017084547.2880694-1-x-professor@outlook.com \
--to=x-professor@outlook.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=axboe@kernel.dk \
--cc=fujita.tomonori@lab.ntt.co.jp \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=mkp@kernel.org \
/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®