From patchwork Thu Feb 2 16:24:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomas Henzl X-Patchwork-Id: 13126544 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D163FC61DA4 for ; Thu, 2 Feb 2023 16:25:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232149AbjBBQZy (ORCPT ); Thu, 2 Feb 2023 11:25:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231546AbjBBQZv (ORCPT ); Thu, 2 Feb 2023 11:25:51 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFE1969B08 for ; Thu, 2 Feb 2023 08:24:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675355097; 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=lvAQrZ5GI20DfhRf7JUyWWGadV3QhvQWMOzJf48p54A=; b=KJBdWzuqc9mF6wy9k4nwR8tKSVuVeFAxlCuRaVnx817eT+pmKVs1gIfMS1M9cdi8PhF849 dZLZS/RHlBzZLzkwroGVTVbQmCbzI4pCQI6UqoWxd9/iOqxYhLgYA8F3x0tnM9ZUGqlGjx zbiDl87RIKSTjOSukJem0EraDxxLS5E= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-221-N_Lz-7i2O7aDSE12ak0VAA-1; Thu, 02 Feb 2023 11:24:53 -0500 X-MC-Unique: N_Lz-7i2O7aDSE12ak0VAA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3E7858027EB; Thu, 2 Feb 2023 16:24:53 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-192-75.brq.redhat.com [10.40.192.75]) by smtp.corp.redhat.com (Postfix) with ESMTP id BA025404BEC0; Thu, 2 Feb 2023 16:24:52 +0000 (UTC) From: Tomas Henzl To: linux-scsi@vger.kernel.org Cc: mikoxyzzz@gmail.com Subject: [PATCH v2 1/4] ses: fix slab-out-of-bounds reported by KASAN in ses_enclosure_data_process Date: Thu, 2 Feb 2023 17:24:48 +0100 Message-Id: <20230202162451.15346-2-thenzl@redhat.com> In-Reply-To: <20230202162451.15346-1-thenzl@redhat.com> References: <20230202162451.15346-1-thenzl@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org A fix for: BUG: KASAN: slab-out-of-bounds in ses_enclosure_data_process+0x949/0xe30 [ses] Read of size 1 at addr ffff88a1b043a451 by task systemd-udevd/3271 Checking after (and before in next loop) addl_desc_ptr[1] is sufficient, we expect the size to be sanitized before first access to addl_desc_ptr[1]. Testing for one more byte shall protect partially the ses_process_descriptor. Signed-off-by: Tomas Henzl --- drivers/scsi/ses.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/scsi/ses.c b/drivers/scsi/ses.c index 0a1734f34587..1ee927ac8603 100644 --- a/drivers/scsi/ses.c +++ b/drivers/scsi/ses.c @@ -603,9 +603,11 @@ static void ses_enclosure_data_process(struct enclosure_device *edev, /* these elements are optional */ type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT || type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT || - type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS)) + type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS)){ addl_desc_ptr += addl_desc_ptr[1] + 2; - + if (addl_desc_ptr + 1 >= ses_dev->page10 + ses_dev->page10_len) + addl_desc_ptr = NULL; + } } } kfree(buf); From patchwork Thu Feb 2 16:24:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomas Henzl X-Patchwork-Id: 13126545 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7C28C636D4 for ; Thu, 2 Feb 2023 16:26:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232222AbjBBQ0C (ORCPT ); Thu, 2 Feb 2023 11:26:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232159AbjBBQZy (ORCPT ); Thu, 2 Feb 2023 11:25:54 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6498818ABE for ; Thu, 2 Feb 2023 08:24:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675355098; 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=GVtro9pKzbqR75MyxTuAM2bo/T2N2giO4SZGiPbUCHQ=; b=b8eY+lWte6C3uXoCHXfLYINaXWZOUDC7RGU9BcoKTrocBwGeVWG+fhVShISwwa1b/cD2kw qP1h9q4ykNCJcfoETx7rBgavIKZGxuFWhMxuxoy5o4AY1gEusWcj3Yd3RaXSrijfK+LHAi MDANQTSzOj898gYCfQYBwZS+tQK1bf4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-133-ofR8oMt4OPe8UZZw8MboEA-1; Thu, 02 Feb 2023 11:24:55 -0500 X-MC-Unique: ofR8oMt4OPe8UZZw8MboEA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EEE0980006E; Thu, 2 Feb 2023 16:24:53 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-192-75.brq.redhat.com [10.40.192.75]) by smtp.corp.redhat.com (Postfix) with ESMTP id 76730404BEC0; Thu, 2 Feb 2023 16:24:53 +0000 (UTC) From: Tomas Henzl To: linux-scsi@vger.kernel.org Cc: mikoxyzzz@gmail.com Subject: [PATCH v2 2/4] ses: fix possible addl_desc_ptr out-of-bounds accesses in ses_enclosure_data_process Date: Thu, 2 Feb 2023 17:24:49 +0100 Message-Id: <20230202162451.15346-3-thenzl@redhat.com> In-Reply-To: <20230202162451.15346-1-thenzl@redhat.com> References: <20230202162451.15346-1-thenzl@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Sanitize possible addl_desc_ptr out-of-bounds accesses in ses_enclosure_data_process. Signed-off-by: Tomas Henzl --- drivers/scsi/ses.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/drivers/scsi/ses.c b/drivers/scsi/ses.c index 1ee927ac8603..896fd4f6e93d 100644 --- a/drivers/scsi/ses.c +++ b/drivers/scsi/ses.c @@ -433,8 +433,8 @@ int ses_match_host(struct enclosure_device *edev, void *data) } #endif /* 0 */ -static void ses_process_descriptor(struct enclosure_component *ecomp, - unsigned char *desc) +static int ses_process_descriptor(struct enclosure_component *ecomp, + unsigned char *desc, int max_desc_len) { int eip = desc[0] & 0x10; int invalid = desc[0] & 0x80; @@ -445,22 +445,32 @@ static void ses_process_descriptor(struct enclosure_component *ecomp, unsigned char *d; if (invalid) - return; + return 0; switch (proto) { case SCSI_PROTOCOL_FCP: if (eip) { + if (max_desc_len <= 7) + return 1; d = desc + 4; slot = d[3]; } break; case SCSI_PROTOCOL_SAS: + if (eip) { + if (max_desc_len <= 27) + return 1; d = desc + 4; slot = d[3]; d = desc + 8; - } else + } else { + if (max_desc_len <= 23) + return 1; d = desc + 4; + } + + /* only take the phy0 addr */ addr = (u64)d[12] << 56 | (u64)d[13] << 48 | @@ -477,6 +487,8 @@ static void ses_process_descriptor(struct enclosure_component *ecomp, } ecomp->slot = slot; scomp->addr = addr; + + return 0; } struct efd { @@ -549,7 +561,7 @@ static void ses_enclosure_data_process(struct enclosure_device *edev, /* skip past overall descriptor */ desc_ptr += len + 4; } - if (ses_dev->page10) + if (ses_dev->page10 && ses_dev->page10_len > 9) addl_desc_ptr = ses_dev->page10 + 8; type_ptr = ses_dev->page1_types; components = 0; @@ -557,6 +569,7 @@ static void ses_enclosure_data_process(struct enclosure_device *edev, for (j = 0; j < type_ptr[1]; j++) { char *name = NULL; struct enclosure_component *ecomp; + int max_desc_len; if (desc_ptr) { if (desc_ptr >= buf + page7_len) { @@ -583,10 +596,14 @@ static void ses_enclosure_data_process(struct enclosure_device *edev, ecomp = &edev->component[components++]; if (!IS_ERR(ecomp)) { - if (addl_desc_ptr) - ses_process_descriptor( - ecomp, - addl_desc_ptr); + if (addl_desc_ptr) { + max_desc_len = ses_dev->page10_len - + (addl_desc_ptr - ses_dev->page10); + if (ses_process_descriptor(ecomp, + addl_desc_ptr, + max_desc_len)) + addl_desc_ptr = NULL; + } if (create) enclosure_component_register( ecomp); From patchwork Thu Feb 2 16:24:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomas Henzl X-Patchwork-Id: 13126542 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 779A3C05027 for ; Thu, 2 Feb 2023 16:25:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231245AbjBBQZv (ORCPT ); Thu, 2 Feb 2023 11:25:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230144AbjBBQZt (ORCPT ); Thu, 2 Feb 2023 11:25:49 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD53D4B19B for ; Thu, 2 Feb 2023 08:24:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675355098; 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=lhZjMiHleEv76N3Nisr/BZ88qX5wCuHWty6zAczN+zc=; b=d0ORsVC9/upyWawEtqFZag4/96D+bxcSzorwrBwuqWZqtSa0Mbrr+pgHPLEPPGLAzpAOqf z0xkyODyfU6TmPh6mUrHTKeB7ptRlJSgvmbU0wMx8OxUp2e0mSqjD92kL0s6/9Lxcr8TG1 IM4kdMrUw8K+4iv27UzU4UnuFfgWDz4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-422-lHWkUoMhMQeFeH92M0xP5A-1; Thu, 02 Feb 2023 11:24:55 -0500 X-MC-Unique: lHWkUoMhMQeFeH92M0xP5A-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AD02D83394C; Thu, 2 Feb 2023 16:24:54 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-192-75.brq.redhat.com [10.40.192.75]) by smtp.corp.redhat.com (Postfix) with ESMTP id 33B5C404BEC0; Thu, 2 Feb 2023 16:24:54 +0000 (UTC) From: Tomas Henzl To: linux-scsi@vger.kernel.org Cc: mikoxyzzz@gmail.com Subject: [PATCH v2 3/4] ses: fix possible desc_ptr out-of-bounds accesses in ses_enclosure_data_process Date: Thu, 2 Feb 2023 17:24:50 +0100 Message-Id: <20230202162451.15346-4-thenzl@redhat.com> In-Reply-To: <20230202162451.15346-1-thenzl@redhat.com> References: <20230202162451.15346-1-thenzl@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Sanitize possible desc_ptr out-of-bounds accesses in ses_enclosure_data_process. Signed-off-by: Tomas Henzl --- drivers/scsi/ses.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/drivers/scsi/ses.c b/drivers/scsi/ses.c index 896fd4f6e93d..dbfe12f63c98 100644 --- a/drivers/scsi/ses.c +++ b/drivers/scsi/ses.c @@ -572,15 +572,19 @@ static void ses_enclosure_data_process(struct enclosure_device *edev, int max_desc_len; if (desc_ptr) { - if (desc_ptr >= buf + page7_len) { + if (desc_ptr + 3 >= buf + page7_len) { desc_ptr = NULL; } else { len = (desc_ptr[2] << 8) + desc_ptr[3]; desc_ptr += 4; - /* Add trailing zero - pushes into - * reserved space */ - desc_ptr[len] = '\0'; - name = desc_ptr; + if (desc_ptr + len > buf + page7_len) + desc_ptr = NULL; + else { + /* Add trailing zero - pushes into + * reserved space */ + desc_ptr[len] = '\0'; + name = desc_ptr; + } } } if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE || From patchwork Thu Feb 2 16:24:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomas Henzl X-Patchwork-Id: 13126543 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 71D03C636D6 for ; Thu, 2 Feb 2023 16:25:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232053AbjBBQZw (ORCPT ); Thu, 2 Feb 2023 11:25:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230233AbjBBQZu (ORCPT ); Thu, 2 Feb 2023 11:25:50 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0366530EF for ; Thu, 2 Feb 2023 08:24:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675355096; 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=3RjW4DOZCvhQd1bXgafs79oHh0ytkPtehsJxnCjunDQ=; b=XBknLssfSC0HDIf0VzeIFhy4Qyi7DqdNUq6LZrOwwT/xEaetZEYt1hOvzKlNonbZ+Oi/0Z aUgGpvr22OQiIYu7N6GWBYKdB3Z786daCt9EqVxWpAGD/Bv9fbC+rlKSXolnwq4Ozf0OYC lqGW24M3woTOqLMnNizQgZ9Fgr9yYb8= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-86-KZpaF6VBNI2RzjwFDGD-XA-1; Thu, 02 Feb 2023 11:24:55 -0500 X-MC-Unique: KZpaF6VBNI2RzjwFDGD-XA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6AF71296A606; Thu, 2 Feb 2023 16:24:55 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-192-75.brq.redhat.com [10.40.192.75]) by smtp.corp.redhat.com (Postfix) with ESMTP id E6076404BEC0; Thu, 2 Feb 2023 16:24:54 +0000 (UTC) From: Tomas Henzl To: linux-scsi@vger.kernel.org Cc: mikoxyzzz@gmail.com Subject: [PATCH v2 4/4] ses: fix slab-out-of-bounds reported by KASAN in ses_intf_remove Date: Thu, 2 Feb 2023 17:24:51 +0100 Message-Id: <20230202162451.15346-5-thenzl@redhat.com> In-Reply-To: <20230202162451.15346-1-thenzl@redhat.com> References: <20230202162451.15346-1-thenzl@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org A fix for: BUG: KASAN: slab-out-of-bounds in ses_intf_remove+0x23f/0x270 [ses] Read of size 8 at addr ffff88a10d32e5d8 by task rmmod/12013 When edev->components is zero, accessing edev->component[0] members is wrong. Signed-off-by: Tomas Henzl --- drivers/scsi/ses.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/scsi/ses.c b/drivers/scsi/ses.c index dbfe12f63c98..7a9eb54e8808 100644 --- a/drivers/scsi/ses.c +++ b/drivers/scsi/ses.c @@ -850,7 +850,8 @@ static void ses_intf_remove_enclosure(struct scsi_device *sdev) kfree(ses_dev->page2); kfree(ses_dev); - kfree(edev->component[0].scratch); + if (edev->components) + kfree(edev->component[0].scratch); put_device(&edev->edev); enclosure_unregister(edev);