Skip to contentSkip to conversation

firmware: arm_scmi: imx: Track copied syslog words

PATCH Applied Sudeep Holla 3 participants 10 messages Oct 28 – Nov 2

Subsystems (3)
lore
Share & tools
lore

Sudeep Holla

author maintainer
#0 lore raw

PATCH 1/3 firmware: arm_scmi: imx: Track copied syslog words

The MISC syslog iterator records the number of returned words plus firmware-reported remaining words in the caller's size output.

The debugfs consumer uses that value to dump its fixed-size buffer. A malformed or inconsistent response can therefore make it read beyond the copied data and allocation.

Return the accumulated number of words copied into the caller's buffer instead. The generic iterator bounds this count to the initial buffer capacity before processing each response.

Fixes 4fa62e80c7db ("firmware: arm_scmi: imx: Support getting syslog of MISC protocol")
---
 drivers/firmware/arm_scmi/vendors/imx/imx-sm-misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/arm_scmi/vendors/imx/imx-sm-misc.c +1 -1
index 637973fb45e6..0c708b2dd496 100644
@@ -438,7 +438,7 @@ static int iter_misc_syslog_update_state(struct scmi_iterator_state *st,
 
 	st->num_returned = RETURNED(r->numlogflags);
 	st->num_remaining = REMAINING(r->numlogflags);
-	*p->size = st->num_returned + st->num_remaining;
+	*p->size = st->desc_index + st->num_returned;
 
 	return 0;
 }
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 2/3 firmware: arm_scmi: imx: Validate MISC control response size

scmi_imx_misc_ctrl_get() uses @num to report the number of values returned by firmware, but it did not retain the caller's value as the capacity of @val.

A response with more values than the caller allocated was copied into @val unchecked. For example, the MQS regmap callback supplies storage for one value. Retain the input capacity and reject responses exceeding it before copying, then update @num only after a successful copy.

Document the in/out @num contract of the exported helper.

Fixes 61c9f03e22fc ("firmware: arm_scmi: Add initial support for i.MX MISC protocol")
---
 .../firmware/arm_scmi/vendors/imx/imx-sm-misc.c  | 16 ++++++++++++----
 include/linux/firmware/imx/sm.h                  |  6 ++++++
 2 files changed, 18 insertions(+), 4 deletions(-)
drivers/firmware/arm_scmi/vendors/imx/imx-sm-misc.c +12 -4
index 0c708b2dd496..f00f7acba845 100644
@@ -273,10 +273,16 @@ static int scmi_imx_misc_ctrl_get(const struct scmi_protocol_handle *ph,
 {
 	struct scmi_imx_misc_ctrl_get_out *out;
 	struct scmi_xfer *t;
+	u32 num_values, max_values;
 	int ret, i;
 	int max_msg_size = ph->hops->get_max_msg_size(ph);
 	int max_num = (max_msg_size - sizeof(*out)) / sizeof(__le32);
 
+	if (!num || !val || !*num)
+		return -EINVAL;
+
+	max_values = *num;
+
 	ret = scmi_imx_misc_ctrl_validate_id(ph, ctrl_id);
 	if (ret)
 		return ret;
@@ -290,16 +296,18 @@ static int scmi_imx_misc_ctrl_get(const struct scmi_protocol_handle *ph,
 	ret = ph->xops->do_xfer(ph, t);
 	if (!ret) {
 		out = t->rx.buf;
-		*num = le32_to_cpu(out->num);
+		num_values = le32_to_cpu(out->num);
 
-		if (*num >= max_num ||
-		    *num * sizeof(__le32) > t->rx.len - sizeof(__le32)) {
+		if (num_values > max_values || num_values >= max_num ||
+		    num_values * sizeof(__le32) > t->rx.len - sizeof(__le32)) {
 			ph->xops->xfer_put(ph, t);
 			return -EINVAL;
 		}
 
-		for (i = 0; i < *num; i++)
+		for (i = 0; i < num_values; i++)
 			val[i] = le32_to_cpu(out->val[i]);
+
+		*num = num_values;
 	}
 
 	ph->xops->xfer_put(ph, t);
include/linux/firmware/imx/sm.h +6 -0
index ba5d93bd6158..0aec7da0ae78 100644
@@ -29,6 +29,12 @@
 #define SCMI_IMX952_CTRL_BYPASS_AUDMIX	8U      /* WAKE AUDMIX */
 
 #if IS_ENABLED(CONFIG_IMX_SCMI_MISC_DRV)
+/**
+ * scmi_imx_misc_ctrl_get() - Read an i.MX MISC control
+ * @id: Control identifier
+ * @num: On entry, capacity of @val in words; on return, words read
+ * @val: Buffer for the control value
+ */
 int scmi_imx_misc_ctrl_get(u32 id, u32 *num, u32 *val);
 int scmi_imx_misc_ctrl_set(u32 id, u32 val);
 #else
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 3/3 firmware: arm_scmi: Unregister notifier on enable failure

scmi_notifier_register() adds the caller's notifier block before enabling the corresponding event at the platform.

If enablement fails, the caller receives an error and can free its notifier block. A concurrent registration can retain and later enable the handler, leaving the failed caller's block in its notification chain. Event delivery can then dereference the freed block.

Remove the notifier block before dropping the handler reference when event enablement fails.

Fixes e7c215f358a3 ("firmware: arm_scmi: Add notification callbacks-registration")
---
 drivers/firmware/arm_scmi/notify.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/firmware/arm_scmi/notify.c +3 -1
index fb60fb07c0ed..e5de0d29c346 100644
@@ -1395,8 +1395,10 @@ static int scmi_notifier_register(const struct scmi_handle *handle,
 	/* Enable events for not pending handlers */
 	if (!IS_HNDL_PENDING(hndl)) {
 		ret = scmi_event_handler_enable_events(hndl);
-		if (ret)
+		if (ret) {
+			blocking_notifier_chain_unregister(&hndl->chain, nb);
 			scmi_put_handler(ni, hndl);
+		}
 	}
 
 	return ret;
Signature · 1 line
2.43.0
Reviewed-by on v1 3/3
#3 lore raw

Re: [PATCH 3/3] firmware: arm_scmi: Unregister notifier on enable failure

Quoted 8 lines · Sudeep Holla

scmi_notifier_register() adds the caller's notifier block before enabling the corresponding event at the platform.

If enablement fails, the caller receives an error and can free its notifier block. A concurrent registration can retain and later enable the handler, leaving the failed caller's block in its notification chain. Event delivery can then dereference the freed block.

Quoted 7 lines

Remove the notifier block before dropping the handler reference when event enablement fails.

Fixes e7c215f358a3 ("firmware: arm_scmi: Add notification callbacks-registration")
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] firmware: arm_scmi: imx: Validate MISC control response size

In reply to Sudeep Holla
Quoted 9 lines · Sudeep Holla

scmi_imx_misc_ctrl_get() uses @num to report the number of values returned by firmware, but it did not retain the caller's value as the capacity of @val.

A response with more values than the caller allocated was copied into @val unchecked. For example, the MQS regmap callback supplies storage for one value. Retain the input capacity and reject responses exceeding it before copying, then update @num only after a successful copy.

Quoted 6 lines

Document the in/out @num contract of the exported helper.

Fixes 61c9f03e22fc ("firmware: arm_scmi: Add initial support for i.MX MISC protocol")
#5 lore raw

Re: [PATCH 2/3] firmware: arm_scmi: imx: Validate MISC control response size

In reply to Sudeep Holla
Quoted 4 lines · Sudeep Holla

scmi_imx_misc_ctrl_get() uses @num to report the number of values returned by firmware, but it did not retain the caller's value as the capacity of @val.

Hi,

Quoted 42 lines

A response with more values than the caller allocated was copied into @val unchecked. For example, the MQS regmap callback supplies storage for one value. Retain the input capacity and reject responses exceeding it before copying, then update @num only after a successful copy.

Document the in/out @num contract of the exported helper.

Fixes 61c9f03e22fc ("firmware: arm_scmi: Add initial support for i.MX MISC protocol")
---
 .../firmware/arm_scmi/vendors/imx/imx-sm-misc.c  | 16 ++++++++++++----
 include/linux/firmware/imx/sm.h                  |  6 ++++++
 2 files changed, 18 insertions(+), 4 deletions(-)
drivers/firmware/arm_scmi/vendors/imx/imx-sm-misc.c +6 -0
index 0c708b2dd496..f00f7acba845 100644
@@ -273,10 +273,16 @@ static int scmi_imx_misc_ctrl_get(const struct scmi_protocol_handle *ph,
 {
 	struct scmi_imx_misc_ctrl_get_out *out;
 	struct scmi_xfer *t;
+	u32 num_values, max_values;
 	int ret, i;
 	int max_msg_size = ph->hops->get_max_msg_size(ph);
 	int max_num = (max_msg_size - sizeof(*out)) / sizeof(__le32);
 
+	if (!num || !val || !*num)
+		return -EINVAL;
+
+	max_values = *num;
+
 	ret = scmi_imx_misc_ctrl_validate_id(ph, ctrl_id);
 	if (ret)
 		return ret;
@@ -290,16 +296,18 @@ static int scmi_imx_misc_ctrl_get(const struct scmi_protocol_handle *ph,
 	ret = ph->xops->do_xfer(ph, t);
 	if (!ret) {
 		out = t->rx.buf;

drivers/firmware/arm_scmi/vendors/imx/imx-sm-misc.c static int scmi_imx_misc_ctrl_get(const struct scmi_protocol_handle *ph, · line 305

Quoted 10 lines
drivers/firmware/arm_scmi/vendors/imx/imx-sm-misc.c +3 -3
-		*num = le32_to_cpu(out->num);
+		num_values = le32_to_cpu(out->num);
 
-		if (*num >= max_num ||
-		    *num * sizeof(__le32) > t->rx.len - sizeof(__le32)) {
+		if (num_values > max_values || num_values >= max_num ||
+		    num_values * sizeof(__le32) > t->rx.len - sizeof(__le32)) {
 			ph->xops->xfer_put(ph, t);
 			return -EINVAL;
 		}

Why does num_values have to be less than max_num, can it not be equal to? Isn't max_num already smaller than max_msg_size due to the initialisation above?

Regards,
Phil

author maintainer
#6 lore raw

Re: [PATCH 2/3] firmware: arm_scmi: imx: Validate MISC control response size

Quoted 53 lines · Philip Radford
Quoted 4 lines · Sudeep Holla

scmi_imx_misc_ctrl_get() uses @num to report the number of values returned by firmware, but it did not retain the caller's value as the capacity of @val.

Hi,

Quoted 45 lines

A response with more values than the caller allocated was copied into @val unchecked. For example, the MQS regmap callback supplies storage for one value. Retain the input capacity and reject responses exceeding it before copying, then update @num only after a successful copy.

Document the in/out @num contract of the exported helper.

Fixes 61c9f03e22fc ("firmware: arm_scmi: Add initial support for i.MX MISC protocol")
---
 .../firmware/arm_scmi/vendors/imx/imx-sm-misc.c  | 16 ++++++++++++----
 include/linux/firmware/imx/sm.h                  |  6 ++++++
 2 files changed, 18 insertions(+), 4 deletions(-)
drivers/firmware/arm_scmi/vendors/imx/imx-sm-misc.c +7 -1
index 0c708b2dd496..f00f7acba845 100644
@@ -273,10 +273,16 @@ static int scmi_imx_misc_ctrl_get(const struct scmi_protocol_handle *ph,
 {
 	struct scmi_imx_misc_ctrl_get_out *out;
 	struct scmi_xfer *t;
+	u32 num_values, max_values;
 	int ret, i;
 	int max_msg_size = ph->hops->get_max_msg_size(ph);
 	int max_num = (max_msg_size - sizeof(*out)) / sizeof(__le32);
 
+	if (!num || !val || !*num)
+		return -EINVAL;
+
+	max_values = *num;
+
 	ret = scmi_imx_misc_ctrl_validate_id(ph, ctrl_id);
 	if (ret)
 		return ret;
@@ -290,16 +296,18 @@ static int scmi_imx_misc_ctrl_get(const struct scmi_protocol_handle *ph,
 	ret = ph->xops->do_xfer(ph, t);
 	if (!ret) {
 		out = t->rx.buf;
-		*num = le32_to_cpu(out->num);
+		num_values = le32_to_cpu(out->num);
 
Quoted 11 lines

drivers/firmware/arm_scmi/vendors/imx/imx-sm-misc.c static int scmi_imx_misc_ctrl_get(const struct scmi_protocol_handle *ph, · line 305

Quoted 7 lines
drivers/firmware/arm_scmi/vendors/imx/imx-sm-misc.c +2 -2
-		if (*num >= max_num ||
-		    *num * sizeof(__le32) > t->rx.len - sizeof(__le32)) {
+		if (num_values > max_values || num_values >= max_num ||
+		    num_values * sizeof(__le32) > t->rx.len - sizeof(__le32)) {
 			ph->xops->xfer_put(ph, t);
 			return -EINVAL;
 		}

Why does num_values have to be less than max_num, can it not be equal to? Isn't max_num already smaller than max_msg_size due to the initialisation above?

Good question, I didn't look at that logic as I blindly retained the existing logic from *num >= max_num to num_values >= max_num.

The scmi_imx_misc_ctrl_eet() see to allow value equal to max_num. It could have been overlooked when we reviewed initially.

Peng, thoughts ?

Signature · 2 lines
Regards,
Sudeep
Reviewed-by on v1 1/3
#7 lore raw
In reply to Sudeep Holla
Quoted 7 lines · Sudeep Holla

The MISC syslog iterator records the number of returned words plus firmware-reported remaining words in the caller's size output.

The debugfs consumer uses that value to dump its fixed-size buffer. A malformed or inconsistent response can therefore make it read beyond the copied data and allocation.

Quoted 6 lines

Return the accumulated number of words copied into the caller's buffer instead. The generic iterator bounds this count to the initial buffer capacity before processing each response.

This should be: https://sashiko.dev/#/bug/linux-67e325a9-473e-450a-a2a0-bf4c0c720624

Quoted 2 lines
Fixes 4fa62e80c7db ("firmware: arm_scmi: imx: Support getting syslog of MISC protocol")
author maintainer
#8 lore raw
Quoted 8 lines · Peng Fan
Quoted 7 lines · Sudeep Holla

The MISC syslog iterator records the number of returned words plus firmware-reported remaining words in the caller's size output.

The debugfs consumer uses that value to dump its fixed-size buffer. A malformed or inconsistent response can therefore make it read beyond the copied data and allocation.

Quoted 9 lines
Quoted 6 lines

Return the accumulated number of words copied into the caller's buffer instead. The generic iterator bounds this count to the initial buffer capacity before processing each response.

This should be: https://sashiko.dev/#/bug/linux-67e325a9-473e-450a-a2a0-bf4c0c720624

Thanks, my bad copy-paste error. Thought it was copied but no 😢.

Quoted 5 lines
Quoted 2 lines
Fixes 4fa62e80c7db ("firmware: arm_scmi: imx: Support getting syslog of MISC protocol")

Thanks!

Signature · 2 lines
Regards,
Sudeep
author maintainer Applied
#9 lore raw
In reply to Sudeep Holla
Quoted 8 lines · Sudeep Holla

The MISC syslog iterator records the number of returned words plus firmware-reported remaining words in the caller's size output.

The debugfs consumer uses that value to dump its fixed-size buffer. A malformed or inconsistent response can therefore make it read beyond the copied data and allocation.

[...]

Applied to sudeep.holla/linux (for-next/scmi/fixes), thanks!

[1/3] firmware: arm_scmi: imx: Track copied syslog words

      https://git.kernel.org/sudeep.holla/c/3baf579ba766
[2/3] firmware: arm_scmi: imx: Validate MISC control response size
      https://git.kernel.org/sudeep.holla/c/4e3652a0ac71
[3/3] firmware: arm_scmi: Unregister notifier on enable failure
      https://git.kernel.org/sudeep.holla/c/b9bb2eb88037
Signature · 2 lines
Regards,
Sudeep