Skip to contentSkip to conversation

firmware: xilinx: fix out-of-bounds access

PATCH In review Arnd Bergmann 3 participants 6 messages Oct 26, 2020 – Oct 27, 2020 Arm/Zynq architecture

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 firmware: xilinx: fix out-of-bounds access

From: Arnd Bergmann <[email protected]>

The zynqmp_pm_set_suspend_mode() and zynqmp_pm_get_trustzone_version() functions pass values as api_id into zynqmp_pm_invoke_fn that are beyond PM_API_MAX, resulting in an out-of-bounds access:

drivers/firmware/xilinx/zynqmp.c: In function 'zynqmp_pm_set_suspend_mode':
drivers/firmware/xilinx/zynqmp.c:150:24: warning: array subscript 2562 is above array bounds of 'u32[64]' {aka 'unsigned int[64]'} [-Warray-bounds]
150 | if (zynqmp_pm_features[api_id] != PM_FEATURE_UNCHECKED)

      |      ~~~~~~~~~~~~~~~~~~^~~~~~~~
drivers/firmware/xilinx/zynqmp.c:28:12: note: while referencing 'zynqmp_pm_features'
   28 | static u32 zynqmp_pm_features[PM_API_MAX];
      |            ^~~~~~~~~~~~~~~~~~

Replace the resulting undefined behavior with an error return. This may break some things that happen to work at the moment but seems better than randomly overwriting kernel data.

I assume we need additional fixes for the two functions that now return an error.

Fixes 76582671eb5d ("firmware: xilinx: Add Zynqmp firmware driver")
Fixes e178df31cf41 ("firmware: xilinx: Implement ZynqMP power management APIs")
---
 drivers/firmware/xilinx/zynqmp.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/firmware/xilinx/zynqmp.c +3 -0
index 8d1ff2454e2e..efb8a66efc68 100644
@@ -147,6 +147,9 @@ static int zynqmp_pm_feature(u32 api_id)
 		return 0;
 
 	/* Return value if feature is already checked */
+	if (api_id > ARRAY_SIZE(zynqmp_pm_features))
+		return PM_FEATURE_INVALID;
+
 	if (zynqmp_pm_features[api_id] != PM_FEATURE_UNCHECKED)
 		return zynqmp_pm_features[api_id];
 
Signature · 7 lines
2.27.0


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#1 lore raw
Quoted 6 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The zynqmp_pm_set_suspend_mode() and zynqmp_pm_get_trustzone_version() functions pass values as api_id into zynqmp_pm_invoke_fn that are beyond PM_API_MAX, resulting in an out-of-bounds access:

Quoted 7 lines

drivers/firmware/xilinx/zynqmp.c: In function 'zynqmp_pm_set_suspend_mode':
drivers/firmware/xilinx/zynqmp.c:150:24: warning: array subscript 2562 is above array bounds of 'u32[64]' {aka 'unsigned int[64]'} [-Warray-bounds]
150 | if (zynqmp_pm_features[api_id] != PM_FEATURE_UNCHECKED)

      |      ~~~~~~~~~~~~~~~~~~^~~~~~~~
drivers/firmware/xilinx/zynqmp.c:28:12: note: while referencing 'zynqmp_pm_features'
   28 | static u32 zynqmp_pm_features[PM_API_MAX];
      |            ^~~~~~~~~~~~~~~~~~

Which CONFIG option/tool is reporting this issue?

Quoted 21 lines

Replace the resulting undefined behavior with an error return. This may break some things that happen to work at the moment but seems better than randomly overwriting kernel data.

I assume we need additional fixes for the two functions that now return an error.

Fixes 76582671eb5d ("firmware: xilinx: Add Zynqmp firmware driver")
Fixes e178df31cf41 ("firmware: xilinx: Implement ZynqMP power management APIs")
---
 drivers/firmware/xilinx/zynqmp.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/firmware/xilinx/zynqmp.c +0 -0
index 8d1ff2454e2e..efb8a66efc68 100644

drivers/firmware/xilinx/zynqmp.c static int zynqmp_pm_feature(u32 api_id) · line 155

Quoted 9 lines
drivers/firmware/xilinx/zynqmp.c +3 -0
@@ -147,6 +147,9 @@ static int zynqmp_pm_feature(u32 api_id)
 		return 0;
 
 	/* Return value if feature is already checked */
+	if (api_id > ARRAY_SIZE(zynqmp_pm_features))
+		return PM_FEATURE_INVALID;
+
 	if (zynqmp_pm_features[api_id] != PM_FEATURE_UNCHECKED)
 		return zynqmp_pm_features[api_id];
 

Definitely good catch but not quite sure what should be correct reaction.
Rajan: Can you please take a look at it with priority?

Thanks,
Michal

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#2 lore raw

Hi Michal,

Quoted 58 lines

-----Original Message-----
From: Michal Simek <[email protected]>
Sent: 27 October 2020 02:53
To: Arnd Bergmann <[email protected]>; Michal Simek <[email protected]>; Rajan Vaja <[email protected]>
Cc: Arnd Bergmann <[email protected]>; Rajan Vaja <[email protected]>; Greg Kroah-Hartman <[email protected]>; Tejas Patel <[email protected]>; [email protected]; linux- [email protected]; git <[email protected]>
Subject: Re: [PATCH] firmware: xilinx: fix out-of-bounds access

Quoted 3 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The zynqmp_pm_set_suspend_mode() and

zynqmp_pm_get_trustzone_version()

Quoted 4 lines

functions pass values as api_id into zynqmp_pm_invoke_fn that are beyond PM_API_MAX, resulting in an out-of-bounds access:

drivers/firmware/xilinx/zynqmp.c: In function

'zynqmp_pm_set_suspend_mode':

Quoted 1 line

drivers/firmware/xilinx/zynqmp.c:150:24: warning: array subscript 2562 is above

array bounds of 'u32[64]' {aka 'unsigned int[64]'} [-Warray-bounds]

Quoted 3 lines

150 | if (zynqmp_pm_features[api_id] != PM_FEATURE_UNCHECKED)

      |      ~~~~~~~~~~~~~~~~~~^~~~~~~~

drivers/firmware/xilinx/zynqmp.c:28:12: note: while referencing

'zynqmp_pm_features'

Quoted 2 lines
   28 | static u32 zynqmp_pm_features[PM_API_MAX];
      |            ^~~~~~~~~~~~~~~~~~

Which CONFIG option/tool is reporting this issue?

Quoted 10 lines

Replace the resulting undefined behavior with an error return. This may break some things that happen to work at the moment but seems better than randomly overwriting kernel data.

I assume we need additional fixes for the two functions that now return an error.

Fixes 76582671eb5d ("firmware: xilinx: Add Zynqmp firmware driver")
Fixes e178df31cf41 ("firmware: xilinx: Implement ZynqMP power management

APIs")

Review

Quoted 6 lines
---
 drivers/firmware/xilinx/zynqmp.c | 3 +++
 1 file changed, 3 insertions(+)
(quoted diff) +0 -0

b/drivers/firmware/xilinx/zynqmp.c

Quoted 6 lines

index 8d1ff2454e2e..efb8a66efc68 100644

drivers/firmware/xilinx/zynqmp.c +0 -0
@@ -147,6 +147,9 @@ static int zynqmp_pm_feature(u32 api_id)
 		return 0;
 
Quoted 11 lines

drivers/firmware/xilinx/zynqmp.c static int zynqmp_pm_feature(u32 api_id) · line 155

Quoted 8 lines
drivers/firmware/xilinx/zynqmp.c +3 -0
 	/* Return value if feature is already checked */
+	if (api_id > ARRAY_SIZE(zynqmp_pm_features))
+		return PM_FEATURE_INVALID;
+
 	if (zynqmp_pm_features[api_id] != PM_FEATURE_UNCHECKED)
 		return zynqmp_pm_features[api_id];
 

Definitely good catch but not quite sure what should be correct reaction.
Rajan: Can you please take a look at it with priority?

[Rajan] Change looks fine to me.

Thanks,
Rajan

Quoted 3 lines

Thanks,
Michal

#3 lore raw
Quoted 71 lines · Rajan Vaja

Hi Michal,

Quoted 58 lines

-----Original Message-----
From: Michal Simek <[email protected]>
Sent: 27 October 2020 02:53
To: Arnd Bergmann <[email protected]>; Michal Simek <[email protected]>; Rajan Vaja <[email protected]>
Cc: Arnd Bergmann <[email protected]>; Rajan Vaja <[email protected]>; Greg Kroah-Hartman <[email protected]>; Tejas Patel <[email protected]>; [email protected]; linux- [email protected]; git <[email protected]>
Subject: Re: [PATCH] firmware: xilinx: fix out-of-bounds access

Quoted 3 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The zynqmp_pm_set_suspend_mode() and

zynqmp_pm_get_trustzone_version()

Quoted 4 lines

functions pass values as api_id into zynqmp_pm_invoke_fn that are beyond PM_API_MAX, resulting in an out-of-bounds access:

drivers/firmware/xilinx/zynqmp.c: In function

'zynqmp_pm_set_suspend_mode':

Quoted 1 line

drivers/firmware/xilinx/zynqmp.c:150:24: warning: array subscript 2562 is above

array bounds of 'u32[64]' {aka 'unsigned int[64]'} [-Warray-bounds]

Quoted 3 lines

150 | if (zynqmp_pm_features[api_id] != PM_FEATURE_UNCHECKED)

      |      ~~~~~~~~~~~~~~~~~~^~~~~~~~

drivers/firmware/xilinx/zynqmp.c:28:12: note: while referencing

'zynqmp_pm_features'

Quoted 2 lines
   28 | static u32 zynqmp_pm_features[PM_API_MAX];
      |            ^~~~~~~~~~~~~~~~~~

Which CONFIG option/tool is reporting this issue?

Quoted 10 lines

Replace the resulting undefined behavior with an error return. This may break some things that happen to work at the moment but seems better than randomly overwriting kernel data.

I assume we need additional fixes for the two functions that now return an error.

Fixes 76582671eb5d ("firmware: xilinx: Add Zynqmp firmware driver")
Fixes e178df31cf41 ("firmware: xilinx: Implement ZynqMP power management

APIs")

Review

Quoted 6 lines
---
 drivers/firmware/xilinx/zynqmp.c | 3 +++
 1 file changed, 3 insertions(+)
(quoted diff) +0 -0

b/drivers/firmware/xilinx/zynqmp.c

Quoted 6 lines

index 8d1ff2454e2e..efb8a66efc68 100644

drivers/firmware/xilinx/zynqmp.c +0 -0
@@ -147,6 +147,9 @@ static int zynqmp_pm_feature(u32 api_id)
 		return 0;
 
Quoted 11 lines

drivers/firmware/xilinx/zynqmp.c static int zynqmp_pm_feature(u32 api_id) · line 155

Quoted 8 lines
drivers/firmware/xilinx/zynqmp.c +3 -0
 	/* Return value if feature is already checked */
+	if (api_id > ARRAY_SIZE(zynqmp_pm_features))
+		return PM_FEATURE_INVALID;
+
 	if (zynqmp_pm_features[api_id] != PM_FEATURE_UNCHECKED)
 		return zynqmp_pm_features[api_id];
 

Definitely good catch but not quite sure what should be correct reaction.
Rajan: Can you please take a look at it with priority?

Quoted 1 line

[Rajan] Change looks fine to me.

as is mentioned above that two functions now returns and error PM_FEATURE_INVALID. Which means that zynqmp_pm_set_suspend_mode() and zynqmp_pm_get_trustzone_version() fail all the time which doesn't look correct.

M

#4 lore raw

Hi Michal,

Quoted 86 lines

-----Original Message-----
From: Michal Simek <[email protected]>
Sent: 27 October 2020 05:27
To: Rajan Vaja <[email protected]>; Arnd Bergmann <[email protected]>
Cc: Arnd Bergmann <[email protected]>; Greg Kroah-Hartman <[email protected]>; Tejas Patel <[email protected]>; linux-arm- [email protected]; [email protected]; git <[email protected]>
Subject: Re: [PATCH] firmware: xilinx: fix out-of-bounds access

Quoted 8 lines · Rajan Vaja

Hi Michal,

Quoted 6 lines

-----Original Message-----
From: Michal Simek <[email protected]>
Sent: 27 October 2020 02:53
To: Arnd Bergmann <[email protected]>; Michal Simek <[email protected]>; Rajan Vaja <[email protected]>
Cc: Arnd Bergmann <[email protected]>; Rajan Vaja <[email protected]>;

Greg

Quoted 10 lines
Quoted 10 lines

Kroah-Hartman <[email protected]>; Tejas Patel <[email protected]>; [email protected]; linux- [email protected]; git <[email protected]>
Subject: Re: [PATCH] firmware: xilinx: fix out-of-bounds access

Quoted 2 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

The zynqmp_pm_set_suspend_mode() and

zynqmp_pm_get_trustzone_version()

Quoted 4 lines

functions pass values as api_id into zynqmp_pm_invoke_fn that are beyond PM_API_MAX, resulting in an out-of-bounds access:

drivers/firmware/xilinx/zynqmp.c: In function

'zynqmp_pm_set_suspend_mode':

Quoted 1 line

drivers/firmware/xilinx/zynqmp.c:150:24: warning: array subscript 2562 is

above

Quoted 15 lines
Quoted 15 lines

array bounds of 'u32[64]' {aka 'unsigned int[64]'} [-Warray-bounds]

Quoted 3 lines

150 | if (zynqmp_pm_features[api_id] != PM_FEATURE_UNCHECKED)

      |      ~~~~~~~~~~~~~~~~~~^~~~~~~~

drivers/firmware/xilinx/zynqmp.c:28:12: note: while referencing

'zynqmp_pm_features'

Quoted 2 lines
   28 | static u32 zynqmp_pm_features[PM_API_MAX];
      |            ^~~~~~~~~~~~~~~~~~

Which CONFIG option/tool is reporting this issue?

Quoted 5 lines

Replace the resulting undefined behavior with an error return. This may break some things that happen to work at the moment but seems better than randomly overwriting kernel data.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

I assume we need additional fixes for the two functions that now return an error.

Fixes 76582671eb5d ("firmware: xilinx: Add Zynqmp firmware driver")
Fixes e178df31cf41 ("firmware: xilinx: Implement ZynqMP power

management

Quoted 25 lines
Quoted 25 lines

APIs")

Review

Quoted 6 lines
---
 drivers/firmware/xilinx/zynqmp.c | 3 +++
 1 file changed, 3 insertions(+)
(quoted diff) +0 -0

b/drivers/firmware/xilinx/zynqmp.c

Quoted 5 lines

index 8d1ff2454e2e..efb8a66efc68 100644

drivers/firmware/xilinx/zynqmp.c static int zynqmp_pm_feature(u32 api_id) · line 155

Quoted 9 lines
drivers/firmware/xilinx/zynqmp.c +3 -0
@@ -147,6 +147,9 @@ static int zynqmp_pm_feature(u32 api_id)
 		return 0;
 
 	/* Return value if feature is already checked */
+	if (api_id > ARRAY_SIZE(zynqmp_pm_features))
+		return PM_FEATURE_INVALID;
+
 	if (zynqmp_pm_features[api_id] != PM_FEATURE_UNCHECKED)
 		return zynqmp_pm_features[api_id];
 

Definitely good catch but not quite sure what should be correct reaction.
Rajan: Can you please take a look at it with priority?

Quoted 7 lines
Quoted 1 line

[Rajan] Change looks fine to me.

as is mentioned above that two functions now returns and error PM_FEATURE_INVALID. Which means that zynqmp_pm_set_suspend_mode() and
zynqmp_pm_get_trustzone_version() fail all the time which doesn't look correct.

[Rajan] Right Michal, I completely missed API which are not in pmufw , API IDs from ATF are different and it doesn't fit in PM_API_MAX range, so I think instead of just single array with PM_API_MAX size, implementation can be updated having, hash table kind of implementation and not directly in array index which is same as API ID.

Thanks,
Rajan

Quoted 2 lines

M

#5 lore raw
In reply to Michal Simek
Quoted 15 lines · Michal Simek
Quoted 6 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The zynqmp_pm_set_suspend_mode() and zynqmp_pm_get_trustzone_version() functions pass values as api_id into zynqmp_pm_invoke_fn that are beyond PM_API_MAX, resulting in an out-of-bounds access:

Quoted 7 lines

drivers/firmware/xilinx/zynqmp.c: In function 'zynqmp_pm_set_suspend_mode':
drivers/firmware/xilinx/zynqmp.c:150:24: warning: array subscript 2562 is above array bounds of 'u32[64]' {aka 'unsigned int[64]'} [-Warray-bounds]
150 | if (zynqmp_pm_features[api_id] != PM_FEATURE_UNCHECKED)

      |      ~~~~~~~~~~~~~~~~~~^~~~~~~~
drivers/firmware/xilinx/zynqmp.c:28:12: note: while referencing 'zynqmp_pm_features'
   28 | static u32 zynqmp_pm_features[PM_API_MAX];
      |            ^~~~~~~~~~~~~~~~~~
Quoted 1 line

Which CONFIG option/tool is reporting this issue?

This is with gcc-10. Commit 44720996e2d7 ("gcc-10: disable 'array-bounds' warning for
now") turned off this warning globally, but most of the reported warnings got fixed in the meantime. I'm trying to take care of the rest so we can enabled it again.

You should be able to reproduce the problem by removing the cc-disable-warning line from the top-level Makefile.

Arnd