The pr_read_keys() interface has a u32 num_keys parameter. The SCSI
PERSISTENT RESERVE IN command has a maximum READ KEYS service action
size of 65536 bytes. Reject num_keys values that are too large to fit
into the SCSI command.
This will become important when pr_read_keys() is exposed to untrusted
userspace via an <linux/pr.h> ioctl.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
drivers/scsi/sd.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 0252d3f6bed17..e436ed977cdb4 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1974,9 +1974,18 @@ static int sd_pr_read_keys(struct block_device *bdev, struct pr_keys *keys_info)
{
int result, i, data_offset, num_copy_keys;
u32 num_keys = keys_info->num_keys;
- int data_len = num_keys * 8 + 8;
+ int data_len;
u8 *data;
+ /*
+ * Each reservation key takes 8 bytes and there is an 8-byte header
+ * before the reservation key list. The total size must fit into the
+ * 16-bit ALLOCATION LENGTH field.
+ */
+ if (num_keys > (USHRT_MAX / 8) - 1)
+ return -EINVAL;
+
+ data_len = num_keys * 8 + 8;
data = kzalloc(data_len, GFP_KERNEL);
if (!data)
return -ENOMEM;
--
2.52.0
On Thu, Nov 27, 2025 at 10:54:21AM -0500, Stefan Hajnoczi wrote: > + /* > + * Each reservation key takes 8 bytes and there is an 8-byte header > + * before the reservation key list. The total size must fit into the > + * 16-bit ALLOCATION LENGTH field. > + */ > + if (num_keys > (USHRT_MAX / 8) - 1) > + return -EINVAL; > + > + data_len = num_keys * 8 + 8; Having the same arithmerics express here in two different ways is a bit odd. I'd expected this to be something like: if (check_mul_overflow(num_keys, 8, &data_len) || data_len > USHRT_MAX) return -EINVAL;
On Mon, Dec 01, 2025 at 07:34:13AM +0100, Christoph Hellwig wrote: > On Thu, Nov 27, 2025 at 10:54:21AM -0500, Stefan Hajnoczi wrote: > > + /* > > + * Each reservation key takes 8 bytes and there is an 8-byte header > > + * before the reservation key list. The total size must fit into the > > + * 16-bit ALLOCATION LENGTH field. > > + */ > > + if (num_keys > (USHRT_MAX / 8) - 1) > > + return -EINVAL; > > + > > + data_len = num_keys * 8 + 8; > > Having the same arithmerics express here in two different ways is a bit > odd. > > I'd expected this to be something like: > > if (check_mul_overflow(num_keys, 8, &data_len) || data_len > USHRT_MAX) > return -EINVAL; Will fix, thanks. Stefan
On Mon, Dec 01, 2025 at 07:34:13AM +0100, Christoph Hellwig wrote: > On Thu, Nov 27, 2025 at 10:54:21AM -0500, Stefan Hajnoczi wrote: > > + /* > > + * Each reservation key takes 8 bytes and there is an 8-byte header > > + * before the reservation key list. The total size must fit into the > > + * 16-bit ALLOCATION LENGTH field. > > + */ > > + if (num_keys > (USHRT_MAX / 8) - 1) > > + return -EINVAL; > > + > > + data_len = num_keys * 8 + 8; > > Having the same arithmerics express here in two different ways is a bit > odd. > > I'd expected this to be something like: > > if (check_mul_overflow(num_keys, 8, &data_len) || data_len > USHRT_MAX) > return -EINVAL; Thanks, will fix. Stefan
On 11/27/25 16:54, Stefan Hajnoczi wrote: > The pr_read_keys() interface has a u32 num_keys parameter. The SCSI > PERSISTENT RESERVE IN command has a maximum READ KEYS service action > size of 65536 bytes. Reject num_keys values that are too large to fit > into the SCSI command. > > This will become important when pr_read_keys() is exposed to untrusted > userspace via an <linux/pr.h> ioctl. > > Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> > --- > drivers/scsi/sd.c | 11 ++++++++++- > 1 file changed, 10 insertions(+), 1 deletion(-) > Reviewed-by: Hannes Reinecke <hare@suse.de> Cheers, Hannes -- Dr. Hannes Reinecke Kernel Storage Architect hare@suse.de +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
© 2016 - 2025 Red Hat, Inc.