Skip to content

Commit ab4fb1d

Browse files
stefanhaRHaxboe
authored andcommitted
scsi: sd: reject invalid pr_read_keys() num_keys values
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> Reviewed-by: Hannes Reinecke <hare@suse.de> Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 48f22f8 commit ab4fb1d

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

drivers/scsi/sd.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1974,9 +1974,19 @@ static int sd_pr_read_keys(struct block_device *bdev, struct pr_keys *keys_info)
19741974
{
19751975
int result, i, data_offset, num_copy_keys;
19761976
u32 num_keys = keys_info->num_keys;
1977-
int data_len = num_keys * 8 + 8;
1977+
int data_len;
19781978
u8 *data;
19791979

1980+
/*
1981+
* Each reservation key takes 8 bytes and there is an 8-byte header
1982+
* before the reservation key list. The total size must fit into the
1983+
* 16-bit ALLOCATION LENGTH field.
1984+
*/
1985+
if (check_mul_overflow(num_keys, 8, &data_len) ||
1986+
check_add_overflow(data_len, 8, &data_len) ||
1987+
data_len > USHRT_MAX)
1988+
return -EINVAL;
1989+
19801990
data = kzalloc(data_len, GFP_KERNEL);
19811991
if (!data)
19821992
return -ENOMEM;

0 commit comments

Comments
 (0)