Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp5115611rwb; Wed, 17 Aug 2022 11:19:16 -0700 (PDT) X-Google-Smtp-Source: AA6agR59lNwd7xCh2v+Z5RtUAmVmOvNnU99sFijkSMYXYd9LJiBXgRTcGeSY+QOnKwP6WvwLvuj8 X-Received: by 2002:a17:902:f548:b0:16f:9649:be69 with SMTP id h8-20020a170902f54800b0016f9649be69mr27245589plf.134.1660760355743; Wed, 17 Aug 2022 11:19:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660760355; cv=none; d=google.com; s=arc-20160816; b=JpU82/wrbksIBaWO5nyK9h8/fG+2YXb4cb82niQU75MDt+b12ypXj6BUL4Y8TIf2Xs q0g3S5ZZ9wfhtcC3SH/oNYR99FrXwnaETMOnRpTaPfx7mah3JQXqTv5gJCKQgIcjnpxh DWPz7NpVSVQffJI00T0mIBU7o9a6AC9hWHYCHlN2r53HGi3xV7C/kZXjmaCFMiN99012 Mw593oyUlOYYzHtn7syhwVywf8JGC0simQ3S0U2IK88BxUBmCP9DGjPCiI+WJXpcpNCy cOJxJJgqSm3Fy7D6Cdeh1Vi6xWHyuPqCTnpoyJooZqtl30w7uawFwOUQinao6JeYZf8P Rw2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=tastvOz6pBRZYtsHNoiCpOyqkCfIZ046X0zkuC0PI5k=; b=rwaiJNmiMdUKrW+mAvUFyrtfBYmIX1Vny+wJHOvbEMiGzYunNoy5yLUe/BOeZZegb3 zyB/W/ue4UsDkreucWmXsmqkWN0kvych+YtQxjl/OfZQ29N3kLAzIKQgZOPhjwG0O6iu EhmMYAdgEOC8MhXyE9NN4tlzGpfIIjh+Gu+xrPWlfHhmkjwLU6eB7nm495Hv7OpHuRi7 hkrRr6AHRN0iwFrOxv52sFsNbNVFCdjp0l1bB0b7cCa8eLaZPqyZtiprxgLwk9ioQrzc p+UXPhQMx8f4vaFnlRC7aQGSDNr5N07u3pFRf2zsc6QQG2mNLQne9IBO39zanNThXyR5 mTCw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u7-20020a170902b28700b0016ee3f81da3si307023plr.606.2022.08.17.11.19.04; Wed, 17 Aug 2022 11:19:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241193AbiHQR2I (ORCPT + 99 others); Wed, 17 Aug 2022 13:28:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241097AbiHQR14 (ORCPT ); Wed, 17 Aug 2022 13:27:56 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C7377A0634 for ; Wed, 17 Aug 2022 10:27:55 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 74CAB1576; Wed, 17 Aug 2022 10:27:56 -0700 (PDT) Received: from e120937-lin.home (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id DAE903F66F; Wed, 17 Aug 2022 10:27:54 -0700 (PDT) From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: sudeep.holla@arm.com, cristian.marussi@arm.com Subject: [PATCH 3/6] firmware: arm_scmi: Harden accesses to Sensor domains Date: Wed, 17 Aug 2022 18:27:28 +0100 Message-Id: <20220817172731.1185305-4-cristian.marussi@arm.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220817172731.1185305-1-cristian.marussi@arm.com> References: <20220817172731.1185305-1-cristian.marussi@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Accessing Sensor domains descriptors by index upon SCMI drivers requests through the SCMI sensor operations interface can potentially lead to out-of-bound violations if the SCMI driver misbehave. Add an internal consistency check in front of such domains descriptors accesses. Signed-off-by: Cristian Marussi --- drivers/firmware/arm_scmi/sensors.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/drivers/firmware/arm_scmi/sensors.c b/drivers/firmware/arm_scmi/sensors.c index 7d0c7476d206..0b5853fa9d87 100644 --- a/drivers/firmware/arm_scmi/sensors.c +++ b/drivers/firmware/arm_scmi/sensors.c @@ -762,6 +762,10 @@ static int scmi_sensor_config_get(const struct scmi_protocol_handle *ph, { int ret; struct scmi_xfer *t; + struct sensors_info *si = ph->get_priv(ph); + + if (sensor_id >= si->num_sensors) + return -EINVAL; ret = ph->xops->xfer_get_init(ph, SENSOR_CONFIG_GET, sizeof(__le32), sizeof(__le32), &t); @@ -771,7 +775,6 @@ static int scmi_sensor_config_get(const struct scmi_protocol_handle *ph, put_unaligned_le32(sensor_id, t->tx.buf); ret = ph->xops->do_xfer(ph, t); if (!ret) { - struct sensors_info *si = ph->get_priv(ph); struct scmi_sensor_info *s = si->sensors + sensor_id; *sensor_config = get_unaligned_le64(t->rx.buf); @@ -788,6 +791,10 @@ static int scmi_sensor_config_set(const struct scmi_protocol_handle *ph, int ret; struct scmi_xfer *t; struct scmi_msg_sensor_config_set *msg; + struct sensors_info *si = ph->get_priv(ph); + + if (sensor_id >= si->num_sensors) + return -EINVAL; ret = ph->xops->xfer_get_init(ph, SENSOR_CONFIG_SET, sizeof(*msg), 0, &t); @@ -800,7 +807,6 @@ static int scmi_sensor_config_set(const struct scmi_protocol_handle *ph, ret = ph->xops->do_xfer(ph, t); if (!ret) { - struct sensors_info *si = ph->get_priv(ph); struct scmi_sensor_info *s = si->sensors + sensor_id; s->sensor_config = sensor_config; @@ -831,8 +837,11 @@ static int scmi_sensor_reading_get(const struct scmi_protocol_handle *ph, int ret; struct scmi_xfer *t; struct scmi_msg_sensor_reading_get *sensor; + struct scmi_sensor_info *s; struct sensors_info *si = ph->get_priv(ph); - struct scmi_sensor_info *s = si->sensors + sensor_id; + + if (sensor_id >= si->num_sensors) + return -EINVAL; ret = ph->xops->xfer_get_init(ph, SENSOR_READING_GET, sizeof(*sensor), 0, &t); @@ -841,6 +850,7 @@ static int scmi_sensor_reading_get(const struct scmi_protocol_handle *ph, sensor = t->tx.buf; sensor->id = cpu_to_le32(sensor_id); + s = si->sensors + sensor_id; if (s->async) { sensor->flags = cpu_to_le32(SENSOR_READ_ASYNC); ret = ph->xops->do_xfer_with_response(ph, t); @@ -895,9 +905,13 @@ scmi_sensor_reading_get_timestamped(const struct scmi_protocol_handle *ph, int ret; struct scmi_xfer *t; struct scmi_msg_sensor_reading_get *sensor; + struct scmi_sensor_info *s; struct sensors_info *si = ph->get_priv(ph); - struct scmi_sensor_info *s = si->sensors + sensor_id; + if (sensor_id >= si->num_sensors) + return -EINVAL; + + s = si->sensors + sensor_id; if (!count || !readings || (!s->num_axis && count > 1) || (s->num_axis && count > s->num_axis)) return -EINVAL; -- 2.32.0