Skip to contentSkip to conversation

media: venus: hfi_cmds: Fix packet size calculation

PATCH Superseded Gustavo A. R. Silva 3 participants 4 messages Jun 1, 2021 – Jun 2, 2021

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: venus: hfi_cmds: Fix packet size calculation

Now that a one-element array was replaced with a flexible-array member in struct hfi_sys_set_property_pkt, use the struct_size() helper to correctly calculate the packet size.

Fixes 701e10b3fd9f ("media: venus: hfi_cmds.h: Replace one-element array with flexible-array member")

---
BTW... it seems that a similar problem is present in
https://lore.kernel.org/linux-hardening/20210211001044.GA69612@embeddedor/
and that is what is causing the regression. I will send v2 of that patch, shortly. Thanks.

 drivers/media/platform/qcom/venus/hfi_cmds.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/media/platform/qcom/venus/hfi_cmds.c +4 -4
index 11a8347e5f5c..c86279e5d6e8 100644
@@ -27,7 +27,7 @@ void pkt_sys_idle_indicator(struct hfi_sys_set_property_pkt *pkt, u32 enable)
 {
 	struct hfi_enable *hfi = (struct hfi_enable *)&pkt->data[1];
 
-	pkt->hdr.size = sizeof(*pkt) + sizeof(*hfi) + sizeof(u32);
+	pkt->hdr.size = struct_size(pkt, data, 2) + sizeof(*hfi);
 	pkt->hdr.pkt_type = HFI_CMD_SYS_SET_PROPERTY;
 	pkt->num_properties = 1;
 	pkt->data[0] = HFI_PROPERTY_SYS_IDLE_INDICATOR;
@@ -39,7 +39,7 @@ void pkt_sys_debug_config(struct hfi_sys_set_property_pkt *pkt, u32 mode,
 {
 	struct hfi_debug_config *hfi;
 
-	pkt->hdr.size = sizeof(*pkt) + sizeof(*hfi) + sizeof(u32);
+	pkt->hdr.size = struct_size(pkt, data, 2) + sizeof(*hfi);
 	pkt->hdr.pkt_type = HFI_CMD_SYS_SET_PROPERTY;
 	pkt->num_properties = 1;
 	pkt->data[0] = HFI_PROPERTY_SYS_DEBUG_CONFIG;
@@ -50,7 +50,7 @@ void pkt_sys_debug_config(struct hfi_sys_set_property_pkt *pkt, u32 mode,
 
 void pkt_sys_coverage_config(struct hfi_sys_set_property_pkt *pkt, u32 mode)
 {
-	pkt->hdr.size = sizeof(*pkt) + sizeof(u32);
+	pkt->hdr.size = struct_size(pkt, data, 2);
 	pkt->hdr.pkt_type = HFI_CMD_SYS_SET_PROPERTY;
 	pkt->num_properties = 1;
 	pkt->data[0] = HFI_PROPERTY_SYS_CONFIG_COVERAGE;
@@ -116,7 +116,7 @@ void pkt_sys_power_control(struct hfi_sys_set_property_pkt *pkt, u32 enable)
 {
 	struct hfi_enable *hfi = (struct hfi_enable *)&pkt->data[1];
 
-	pkt->hdr.size = sizeof(*pkt) + sizeof(*hfi) + sizeof(u32);
+	pkt->hdr.size = struct_size(pkt, data, 2) + sizeof(*hfi);
 	pkt->hdr.pkt_type = HFI_CMD_SYS_SET_PROPERTY;
 	pkt->num_properties = 1;
 	pkt->data[0] = HFI_PROPERTY_SYS_CODEC_POWER_PLANE_CTRL;
Signature · 1 line
2.27.0
#1 lore raw
Quoted 20 lines · Gustavo A. R. Silva

Now that a one-element array was replaced with a flexible-array member in struct hfi_sys_set_property_pkt, use the struct_size() helper to correctly calculate the packet size.

Fixes 701e10b3fd9f ("media: venus: hfi_cmds.h: Replace one-element array with flexible-array member")

---
BTW... it seems that a similar problem is present in
https://lore.kernel.org/linux-hardening/20210211001044.GA69612@embeddedor/
and that is what is causing the regression. I will send v2 of that patch, shortly. Thanks.

 drivers/media/platform/qcom/venus/hfi_cmds.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/media/platform/qcom/venus/hfi_cmds.c +0 -0
index 11a8347e5f5c..c86279e5d6e8 100644

drivers/media/platform/qcom/venus/hfi_cmds.c void pkt_sys_idle_indicator(struct hfi_sys_set_property_pkt *pkt, u32 enable) · line 30

Quoted 5 lines
drivers/media/platform/qcom/venus/hfi_cmds.c +1 -1
@@ -27,7 +27,7 @@ void pkt_sys_idle_indicator(struct hfi_sys_set_property_pkt *pkt, u32 enable)
 {
 	struct hfi_enable *hfi = (struct hfi_enable *)&pkt->data[1];
 
-	pkt->hdr.size = sizeof(*pkt) + sizeof(*hfi) + sizeof(u32);
+	pkt->hdr.size = struct_size(pkt, data, 2) + sizeof(*hfi);

I think this should be "1" not "2".

(i.e. there is a single "data" item, followed by an entire *hfi (which starts immediate after data[0]).

drivers/media/platform/qcom/venus/hfi_cmds.c void pkt_sys_debug_config(struct hfi_sys_set_property_pkt *pkt, u32 mode, · line 42

Quoted 9 lines
drivers/media/platform/qcom/venus/hfi_cmds.c +1 -1
 	pkt->hdr.pkt_type = HFI_CMD_SYS_SET_PROPERTY;
 	pkt->num_properties = 1;
 	pkt->data[0] = HFI_PROPERTY_SYS_IDLE_INDICATOR;
@@ -39,7 +39,7 @@ void pkt_sys_debug_config(struct hfi_sys_set_property_pkt *pkt, u32 mode,
 {
 	struct hfi_debug_config *hfi;
 
-	pkt->hdr.size = sizeof(*pkt) + sizeof(*hfi) + sizeof(u32);
+	pkt->hdr.size = struct_size(pkt, data, 2) + sizeof(*hfi);

Same here.

drivers/media/platform/qcom/venus/hfi_cmds.c void pkt_sys_debug_config(struct hfi_sys_set_property_pkt *pkt, u32 mode, · line 53

Quoted 9 lines
drivers/media/platform/qcom/venus/hfi_cmds.c +1 -1
 	pkt->hdr.pkt_type = HFI_CMD_SYS_SET_PROPERTY;
 	pkt->num_properties = 1;
 	pkt->data[0] = HFI_PROPERTY_SYS_DEBUG_CONFIG;
@@ -50,7 +50,7 @@ void pkt_sys_debug_config(struct hfi_sys_set_property_pkt *pkt, u32 mode,
 
 void pkt_sys_coverage_config(struct hfi_sys_set_property_pkt *pkt, u32 mode)
 {
-	pkt->hdr.size = sizeof(*pkt) + sizeof(u32);
+	pkt->hdr.size = struct_size(pkt, data, 2);

This looks correct.

drivers/media/platform/qcom/venus/hfi_cmds.c void pkt_sys_power_control(struct hfi_sys_set_property_pkt *pkt, u32 enable) · line 119

Quoted 9 lines
drivers/media/platform/qcom/venus/hfi_cmds.c +1 -1
 	pkt->hdr.pkt_type = HFI_CMD_SYS_SET_PROPERTY;
 	pkt->num_properties = 1;
 	pkt->data[0] = HFI_PROPERTY_SYS_CONFIG_COVERAGE;
@@ -116,7 +116,7 @@ void pkt_sys_power_control(struct hfi_sys_set_property_pkt *pkt, u32 enable)
 {
 	struct hfi_enable *hfi = (struct hfi_enable *)&pkt->data[1];
 
-	pkt->hdr.size = sizeof(*pkt) + sizeof(*hfi) + sizeof(u32);
+	pkt->hdr.size = struct_size(pkt, data, 2) + sizeof(*hfi);

Also 1.

Quoted 6 lines
drivers/media/platform/qcom/venus/hfi_cmds.c +0 -0
 	pkt->hdr.pkt_type = HFI_CMD_SYS_SET_PROPERTY;
 	pkt->num_properties = 1;
 	pkt->data[0] = HFI_PROPERTY_SYS_CODEC_POWER_PLANE_CTRL;
Signature · 1 line
2.27.0
Signature · 1 line
Kees Cook
#2 lore raw
Quoted 24 lines · Kees Cook
Quoted 23 lines · Gustavo A. R. Silva

Now that a one-element array was replaced with a flexible-array member in struct hfi_sys_set_property_pkt, use the struct_size() helper to correctly calculate the packet size.

Fixes 701e10b3fd9f ("media: venus: hfi_cmds.h: Replace one-element array with flexible-array member")

---
BTW... it seems that a similar problem is present in
https://lore.kernel.org/linux-hardening/20210211001044.GA69612@embeddedor/
and that is what is causing the regression. I will send v2 of that patch, shortly. Thanks.

 drivers/media/platform/qcom/venus/hfi_cmds.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/media/platform/qcom/venus/hfi_cmds.c +0 -0
index 11a8347e5f5c..c86279e5d6e8 100644
@@ -27,7 +27,7 @@ void pkt_sys_idle_indicator(struct hfi_sys_set_property_pkt *pkt, u32 enable)
 {
 	struct hfi_enable *hfi = (struct hfi_enable *)&pkt->data[1];
 
Quoted 7 lines

drivers/media/platform/qcom/venus/hfi_cmds.c void pkt_sys_idle_indicator(struct hfi_sys_set_property_pkt *pkt, u32 enable) · line 30

Quoted 2 lines
drivers/media/platform/qcom/venus/hfi_cmds.c +1 -1
-	pkt->hdr.size = sizeof(*pkt) + sizeof(*hfi) + sizeof(u32);
+	pkt->hdr.size = struct_size(pkt, data, 2) + sizeof(*hfi);

I think this should be "1" not "2".

(i.e. there is a single "data" item, followed by an entire *hfi (which starts immediate after data[0]).

Yeah; I see your point. Here I just wanted to preserve the exact same size as the original code, which turns out has a "benign" off-by-one issue.

I'll fix it up and respin.

Thanks!
--
Gustavo

Quoted 44 lines

drivers/media/platform/qcom/venus/hfi_cmds.c void pkt_sys_debug_config(struct hfi_sys_set_property_pkt *pkt, u32 mode, · line 42

Quoted 10 lines
drivers/media/platform/qcom/venus/hfi_cmds.c +1 -1
 	pkt->hdr.pkt_type = HFI_CMD_SYS_SET_PROPERTY;
 	pkt->num_properties = 1;
 	pkt->data[0] = HFI_PROPERTY_SYS_IDLE_INDICATOR;
@@ -39,7 +39,7 @@ void pkt_sys_debug_config(struct hfi_sys_set_property_pkt *pkt, u32 mode,
 {
 	struct hfi_debug_config *hfi;
 
-	pkt->hdr.size = sizeof(*pkt) + sizeof(*hfi) + sizeof(u32);
+	pkt->hdr.size = struct_size(pkt, data, 2) + sizeof(*hfi);

Same here.

drivers/media/platform/qcom/venus/hfi_cmds.c void pkt_sys_debug_config(struct hfi_sys_set_property_pkt *pkt, u32 mode, · line 53

Quoted 9 lines
drivers/media/platform/qcom/venus/hfi_cmds.c +1 -1
 	pkt->hdr.pkt_type = HFI_CMD_SYS_SET_PROPERTY;
 	pkt->num_properties = 1;
 	pkt->data[0] = HFI_PROPERTY_SYS_DEBUG_CONFIG;
@@ -50,7 +50,7 @@ void pkt_sys_debug_config(struct hfi_sys_set_property_pkt *pkt, u32 mode,
 
 void pkt_sys_coverage_config(struct hfi_sys_set_property_pkt *pkt, u32 mode)
 {
-	pkt->hdr.size = sizeof(*pkt) + sizeof(u32);
+	pkt->hdr.size = struct_size(pkt, data, 2);

This looks correct.

drivers/media/platform/qcom/venus/hfi_cmds.c void pkt_sys_power_control(struct hfi_sys_set_property_pkt *pkt, u32 enable) · line 119

Quoted 9 lines
drivers/media/platform/qcom/venus/hfi_cmds.c +1 -1
 	pkt->hdr.pkt_type = HFI_CMD_SYS_SET_PROPERTY;
 	pkt->num_properties = 1;
 	pkt->data[0] = HFI_PROPERTY_SYS_CONFIG_COVERAGE;
@@ -116,7 +116,7 @@ void pkt_sys_power_control(struct hfi_sys_set_property_pkt *pkt, u32 enable)
 {
 	struct hfi_enable *hfi = (struct hfi_enable *)&pkt->data[1];
 
-	pkt->hdr.size = sizeof(*pkt) + sizeof(*hfi) + sizeof(u32);
+	pkt->hdr.size = struct_size(pkt, data, 2) + sizeof(*hfi);

Also 1.

Quoted 6 lines
drivers/media/platform/qcom/venus/hfi_cmds.c +0 -0
 	pkt->hdr.pkt_type = HFI_CMD_SYS_SET_PROPERTY;
 	pkt->num_properties = 1;
 	pkt->data[0] = HFI_PROPERTY_SYS_CODEC_POWER_PLANE_CTRL;
Signature · 1 line
2.27.0
#3 lore raw
Quoted 32 lines · Gustavo A. R. Silva
Quoted 29 lines · Kees Cook
Quoted 20 lines · Gustavo A. R. Silva

Now that a one-element array was replaced with a flexible-array member in struct hfi_sys_set_property_pkt, use the struct_size() helper to correctly calculate the packet size.

Fixes 701e10b3fd9f ("media: venus: hfi_cmds.h: Replace one-element array with flexible-array member")

---
BTW... it seems that a similar problem is present in
https://lore.kernel.org/linux-hardening/20210211001044.GA69612@embeddedor/
and that is what is causing the regression. I will send v2 of that patch, shortly. Thanks.

 drivers/media/platform/qcom/venus/hfi_cmds.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/media/platform/qcom/venus/hfi_cmds.c +0 -0
index 11a8347e5f5c..c86279e5d6e8 100644

drivers/media/platform/qcom/venus/hfi_cmds.c void pkt_sys_idle_indicator(struct hfi_sys_set_property_pkt *pkt, u32 enable) · line 30

Quoted 5 lines
drivers/media/platform/qcom/venus/hfi_cmds.c +1 -1
@@ -27,7 +27,7 @@ void pkt_sys_idle_indicator(struct hfi_sys_set_property_pkt *pkt, u32 enable)
 {
 	struct hfi_enable *hfi = (struct hfi_enable *)&pkt->data[1];
 
-	pkt->hdr.size = sizeof(*pkt) + sizeof(*hfi) + sizeof(u32);
+	pkt->hdr.size = struct_size(pkt, data, 2) + sizeof(*hfi);

I think this should be "1" not "2".

Quoted 5 lines
Quoted 2 lines

(i.e. there is a single "data" item, followed by an entire *hfi (which starts immediate after data[0]).

Yeah; I see your point. Here I just wanted to preserve the exact same size as the original code, which turns out has a "benign" off-by-one issue.

And the reason why I say it's a "benign" off-by-one (which is actually off-by-four-bytes) issue is because of this piece of code:

drivers/media/platform/qcom/venus/hfi_venus.c:
864 static int venus_sys_set_idle_message(struct venus_hfi_device *hdev,
865 bool enable)
866 {

 867         struct hfi_sys_set_property_pkt *pkt;
 868         u8 packet[IFACEQ_VAR_SMALL_PKT_SIZE];
 869         int ret;

870
871 if (!enable)
872 return 0;
873
874 pkt = (struct hfi_sys_set_property_pkt *)packet;
875
876 pkt_sys_idle_indicator(pkt, enable);
877
878 ret = venus_iface_cmdq_write(hdev, pkt, false);
879 if (ret)
880 return ret;
881
882 return 0;
883 }

IFACEQ_VAR_SMALL_PKT_SIZE at line 868 is of size 100, which is greater than

sizeof(*pkt) + sizeof(*hfi) + sizeof(u32); in the original code:

drivers/media/platform/qcom/venus/hfi_cmds.c:
26 void pkt_sys_idle_indicator(struct hfi_sys_set_property_pkt *pkt, u32 enable) 27 {
28 struct hfi_enable *hfi = (struct hfi_enable *)&pkt->data[1];
29

  30         pkt->hdr.size = sizeof(*pkt) + sizeof(*hfi) + sizeof(u32);
  31         pkt->hdr.pkt_type = HFI_CMD_SYS_SET_PROPERTY;
  32         pkt->num_properties = 1;
  33         pkt->data[0] = HFI_PROPERTY_SYS_IDLE_INDICATOR;
  34         hfi->enable = enable;

35 }

--
Gustavo

Quoted 5 lines

I'll fix it up and respin.

Thanks!
--
Gustavo