From nobody Mon Dec 1 23:33:19 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 327AA285C89 for ; Mon, 1 Dec 2025 21:43:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764625424; cv=none; b=YJXJSWJhNeAVNElCsGfT1HVzOoS9uuOughmQeR2Eripzqfd0DaCNA9d1UBO7LwdSk+LY9T6Ga20zN/BsSsYKY04uSguJWLS3gfY3EQeKIqQzxRPIpONfD/NZrOsyHQifxa2vc/KdXN+Q2bFKrbaYJtPxZBWUNa82ezikCdbgZ34= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764625424; c=relaxed/simple; bh=TR0EdIYUty1k7XAJrVdGgQmooXt5n/aX214Ma7BluEA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DNkKHpR9W1pYJn5nIcHBpPIm4kxPUSBu1PMxvo3lCmDu8f+3oqgf1wJdl+Mqys2cA56Yq87rE27R/yAVG5nU9uA9ItvWrQWwNzaq0cy4uZcW8lhSYhXzL+zVglEVRg+YrVIDNelf6yB2awzwT84CEfTqRu0KpBC0I+TrhBZ1aMw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=TzhYJigm; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="TzhYJigm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1764625422; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PU3aIbnMU+BuREK+bEcl6eAdFWIrI+XOFPkJF/Fkm1k=; b=TzhYJigmjLBYeoDv2DQ/27soAHAQz4hrh2zu5LZWHHcFCbzzpwl6q+XhKlbLdF+QSZmRtX Y+Gje7mQPiGa2cRC/yhEU8LEeZrsv+UqFLDeLSYbJ/Fc3B9dpwCK5nqRNtrKSQNjvseele fxzFV5McIpT+HmnJbnTP2PFO7eqLrms= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-684-_SWH00H-NZm_sx_DcSsCYA-1; Mon, 01 Dec 2025 16:43:37 -0500 X-MC-Unique: _SWH00H-NZm_sx_DcSsCYA-1 X-Mimecast-MFC-AGG-ID: _SWH00H-NZm_sx_DcSsCYA_1764625415 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 021DA180048E; Mon, 1 Dec 2025 21:43:35 +0000 (UTC) Received: from localhost (unknown [10.2.16.172]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7D34330001A4; Mon, 1 Dec 2025 21:43:33 +0000 (UTC) From: Stefan Hajnoczi To: linux-block@vger.kernel.org Cc: Christoph Hellwig , linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, "James E.J. Bottomley" , "Martin K. Petersen" , Keith Busch , Jens Axboe , Sagi Grimberg , Mike Christie , Krzysztof Kozlowski , linux-nvme@lists.infradead.org, Stefan Hajnoczi Subject: [PATCH v3 1/4] scsi: sd: reject invalid pr_read_keys() num_keys values Date: Mon, 1 Dec 2025 16:43:26 -0500 Message-ID: <20251201214329.933945-2-stefanha@redhat.com> In-Reply-To: <20251201214329.933945-1-stefanha@redhat.com> References: <20251201214329.933945-1-stefanha@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" 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 ioctl. Signed-off-by: Stefan Hajnoczi --- drivers/scsi/sd.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 0252d3f6bed17..32ae4898cea7c 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -1974,9 +1974,19 @@ 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 =3D keys_info->num_keys; - int data_len =3D num_keys * 8 + 8; + int data_len; u8 *data; =20 + /* + * 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 (check_mul_overflow(num_keys, 8, &data_len) || + check_add_overflow(data_len, 8, &data_len) || + data_len > USHRT_MAX) + return -EINVAL; + data =3D kzalloc(data_len, GFP_KERNEL); if (!data) return -ENOMEM; --=20 2.52.0