platform/x86/amd/pmf: Do not use readl() for policy buffer access
Subsystems (2)
PATCH v2 1/2 platform/x86/amd/pmf: Do not use readl() for policy buffer access
The policy buffer is allocated using normal memory allocation functions, so readl() should not be used on it.
Compile-tested only.
---
Changes since v1:
- get the full dword instead of only 8 bits
--- drivers/platform/x86/amd/pmf/tee-if.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/platform/x86/amd/pmf/tee-if.c +2 -2
@@ -249,8 +249,8 @@ static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev)u32 cookie, length;int res; - cookie =readl (dev->policy_buf +POLICY_COOKIE_OFFSET ); - length =readl (dev->policy_buf +POLICY_COOKIE_LEN ); + cookie = *(u32 *)(dev->policy_buf +POLICY_COOKIE_OFFSET ); + length = *(u32 *)(dev->policy_buf +POLICY_COOKIE_LEN );if (cookie !=POLICY_SIGN_COOKIE || !length)return -EINVAL ;
--
2.39.2
PATCH v2 2/2 platform/x86/amd/pmf: Fix possible out-of-bound memory accesses
The length of the policy buffer is not validated before accessing it, which means that multiple out-of-bounds memory accesses can occur.
This is especially bad since userspace can load policy binaries over debugfs.
Compile-tested only.
---
Changes since v1:
- check if the policy buffer also has enough room for storing the length
--- drivers/platform/x86/amd/pmf/tee-if.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/platform/x86/amd/pmf/tee-if.c +6 -0
@@ -249,12 +249,18 @@ static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev)u32 cookie, length;int res; +if (dev->policy_sz <POLICY_COOKIE_LEN +sizeof (length)) +return -EINVAL ; + cookie = *(u32 *)(dev->policy_buf +POLICY_COOKIE_OFFSET ); length = *(u32 *)(dev->policy_buf +POLICY_COOKIE_LEN );if (cookie !=POLICY_SIGN_COOKIE || !length)return -EINVAL ; +if (dev->policy_sz < length +512 ) +return -EINVAL ; +/* Update the actual length */ dev->policy_sz = length +512 ; res =amd_pmf_invoke_cmd_init (dev);
--
2.39.2
Ilpo Järvinen
maintainerRe: [PATCH v2 2/2] platform/x86/amd/pmf: Fix possible out-of-bound memory accesses
Hi Shyam & Armin,
Shyam, please take a look at the question below.
drivers/platform/x86/amd/pmf/tee-if.c static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev) · line 256
Quoted 11 lines · Armin Wolf
The length of the policy buffer is not validated before accessing it, which means that multiple out-of-bounds memory accesses can occur.
This is especially bad since userspace can load policy binaries over debugfs.
drivers/platform/x86/amd/pmf/tee-if.c +3
-0+if (dev->policy_sz <POLICY_COOKIE_LEN +sizeof (length)) +return -EINVAL ; + cookie = *(u32 *)(dev->policy_buf +POLICY_COOKIE_OFFSET ); length = *(u32 *)(dev->policy_buf +POLICY_COOKIE_LEN );
This starts to feel like adding a struct for the header(?) would be better course of action here as then one could compare against sizeof(*header) and avoid all those casts (IMO, just access the header fields directly w/o the local variables).
Shyam, do you think a struct makes sense here? There's some header in this policy, right?
There are more thing to address here...
1) amd_pmf_start_policy_engine() function returns -EINVAL & res that is TA_PMF_* which inconsistent in type of the return value
2) Once 1) is fixed, the caller shadowing the return code can be fixed as well:
ret = amd_pmf_start_policy_engine(dev);
if (ret)
return -EINVAL;Signature · 1 line
i.
Shyam Sundar S K
maintainerRe: [PATCH v2 2/2] platform/x86/amd/pmf: Fix possible out-of-bound memory accesses
Hi Ilpo,
Quoted 12 lines · Ilpo Järvinen
Hi Shyam & Armin,
Shyam, please take a look at the question below.
Quoted 5 lines · Armin Wolf
The length of the policy buffer is not validated before accessing it, which means that multiple out-of-bounds memory accesses can occur.
This is especially bad since userspace can load policy binaries over debugfs.
IMO, this patch is not required, reason being:
- the debugfs patch gets created only when CONFIG_AMD_PMF_DEBUG is enabled.
- Sideload of policy binaries is only supported with a valid signing key. (think like this can be tested & verified within AMD environment)
- Also, in amd_pmf_get_pb_data() there are boundary conditions that are being checked. Is that not sufficient enough?
Quoted 10 lines
drivers/platform/x86/amd/pmf/tee-if.c static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev) · line 256
Quoted 5 lines
drivers/platform/x86/amd/pmf/tee-if.c +3
-0+if (dev->policy_sz <POLICY_COOKIE_LEN +sizeof (length)) +return -EINVAL ; + cookie = *(u32 *)(dev->policy_buf +POLICY_COOKIE_OFFSET ); length = *(u32 *)(dev->policy_buf +POLICY_COOKIE_LEN );This starts to feel like adding a struct for the header(?) would be better course of action here as then one could compare against
sizeof(*header)and avoid all those casts (IMO, just access the header fields directly w/o the local variables).
Not sure if I get your question clearly. Can you elaborate a bit more on the struct you are envisioning?
but IHMO, we actually don't need a struct - as all that we would need is to make sure the signing cookie is part of the policy binary and leave the rest of the error handling to ASP/TEE modules (we can rely on the feedback from those modules).
Quoted 3 lines
Shyam, do you think a struct makes sense here? There's some header in this policy, right?
Yes, the policy binary on a whole has multiple sections within it and there are multiple headers (like signing, OEM header, etc).
But that might be not real interest to the PMF driver. The only thing the driver has to make sure is that the policy binary going into ASP (AMD Secure Processor) is with in the limits and has a valid signing cookie. So this part is already taken care in the current code.
Quoted 7 lines
There are more thing to address here...
1)
amd_pmf_start_policy_engine()function returns-EINVAL& res that is TA_PMF_* which inconsistent in type of the return value
ah! it has mix of both int and u32 :-)
Armin, would you like to amend this in your current series? or else I will submit a change for this in my next series.
Thanks,
Shyam
Quoted 7 lines
2) Once 1) is fixed, the caller shadowing the return code can be fixed as well:
ret = amd_pmf_start_policy_engine(dev); if (ret) return -EINVAL;
Re: [PATCH v2 2/2] platform/x86/amd/pmf: Fix possible out-of-bound memory accesses
Am 28.02.24 um 12:16 schrieb Shyam Sundar S K:
Quoted 14 lines
Hi Ilpo,
Quoted 11 lines · Ilpo Järvinen
Hi Shyam & Armin,
Shyam, please take a look at the question below.
Quoted 5 lines · Armin Wolf
The length of the policy buffer is not validated before accessing it, which means that multiple out-of-bounds memory accesses can occur.
This is especially bad since userspace can load policy binaries over debugfs.
Quoted 7 lines
IMO, this patch is not required, reason being:
- the debugfs patch gets created only whenCONFIG_AMD_PMF_DEBUGis enabled.
- Sideload of policy binaries is only supported with a valid signing key. (think like this can be tested & verified within AMD environment)
- Also, inamd_pmf_get_pb_data()there are boundary conditions that are being checked. Is that not sufficient enough?
IMHO, amd_pmf_get_pb_data() only checks if the length of the binary is between 0 and the maximum buffer size.
If for example the binary contains only 4 bytes, then there will be an out-of-bounds access when trying to read the cookie and length.
Or if the length is bigger than the binary buffer, then the driver just updates the buffer length even if the buffer is too small.
I think the driver should catch such cases and return an error.
(Please note that we are talking about the binary buffer, not the internal structure of the remaining policy binary itself).
Quoted 11 lines
Quoted 9 lines
drivers/platform/x86/amd/pmf/tee-if.c static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev) · line 256
Quoted 5 lines
drivers/platform/x86/amd/pmf/tee-if.c +3
-0+if (dev->policy_sz <POLICY_COOKIE_LEN +sizeof (length)) +return -EINVAL ; + cookie = *(u32 *)(dev->policy_buf +POLICY_COOKIE_OFFSET ); length = *(u32 *)(dev->policy_buf +POLICY_COOKIE_LEN );This starts to feel like adding a struct for the header(?) would be better course of action here as then one could compare against
sizeof(*header)and avoid all those casts (IMO, just access the header fields directly w/o the local variables).Not sure if I get your question clearly. Can you elaborate a bit more on the struct you are envisioning?
I think he envisions something like this:
struct __packed cookie_header {
u32 magic;
u32 length;
};Quoted 22 lines
but IHMO, we actually don't need a struct - as all that we would need is to make sure the signing cookie is part of the policy binary and leave the rest of the error handling to ASP/TEE modules (we can rely on the feedback from those modules).
Quoted 2 lines
Shyam, do you think a struct makes sense here? There's some header in this policy, right?
Yes, the policy binary on a whole has multiple sections within it and there are multiple headers (like signing, OEM header, etc).
But that might be not real interest to the PMF driver. The only thing the driver has to make sure is that the policy binary going into ASP (AMD Secure Processor) is with in the limits and has a valid signing cookie. So this part is already taken care in the current code.
Quoted 6 lines
There are more thing to address here...
1)
amd_pmf_start_policy_engine()function returns-EINVAL& res that is TA_PMF_* which inconsistent in type of the return value
Quoted 7 lines
ah! it has mix of both int and u32 :-)
Armin, would you like to amend this in your current series? or else I will submit a change for this in my next series.
Thanks,
Shyam
I can do so, but i will be unable to send a new patch series for the rest of this week.
Thanks,
Armin Wolf
Quoted 7 lines
Quoted 7 lines
2) Once 1) is fixed, the caller shadowing the return code can be fixed as
well: ret = amd_pmf_start_policy_engine(dev); if (ret) return -EINVAL;
Ilpo Järvinen
maintainerRe: [PATCH v2 2/2] platform/x86/amd/pmf: Fix possible out-of-bound memory accesses
Quoted 23 lines · Armin Wolf
Am 28.02.24 um 12:16 schrieb Shyam Sundar S K:
Quoted 8 lines · Ilpo Järvinen
Quoted 7 lines · Armin Wolf
Quoted 6 lines
The length of the policy buffer is not validated before accessing it, which means that multiple out-of-bounds memory accesses can occur.
This is especially bad since userspace can load policy binaries over debugfs.
Quoted 7 lines
IMO, this patch is not required, reason being:
- the debugfs patch gets created only whenCONFIG_AMD_PMF_DEBUGis enabled.
- Sideload of policy binaries is only supported with a valid signing key. (think like this can be tested & verified within AMD environment)
- Also, inamd_pmf_get_pb_data()there are boundary conditions that are being checked. Is that not sufficient enough?IMHO,
amd_pmf_get_pb_data()only checks if the length of the binary is between 0 and the maximum buffer size.If for example the binary contains only 4 bytes, then there will be an out-of-bounds access when trying to read the cookie and length.
Quoted 7 lines
Or if the length is bigger than the binary buffer, then the driver just updates the buffer length even if the buffer is too small.
I think the driver should catch such cases and return an error.
(Please note that we are talking about the binary buffer, not the internal structure of the remaining policy binary itself).
Yes. Out of bound accesses are not okay during validation even if the binary itself would get rejected at a later stage. It doesn't matter if the interface is only for debug or wider scope.
Quoted 12 lines
Quoted 11 lines
Quoted 9 lines
drivers/platform/x86/amd/pmf/tee-if.c static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev) · line 256
Quoted 5 lines
drivers/platform/x86/amd/pmf/tee-if.c +3
-0+if (dev->policy_sz <POLICY_COOKIE_LEN +sizeof (length)) +return -EINVAL ; + cookie = *(u32 *)(dev->policy_buf +POLICY_COOKIE_OFFSET ); length = *(u32 *)(dev->policy_buf +POLICY_COOKIE_LEN );This starts to feel like adding a struct for the header(?) would be better course of action here as then one could compare against
sizeof(*header)and avoid all those casts (IMO, just access the header fields directly w/o the local variables).Not sure if I get your question clearly. Can you elaborate a bit more on the struct you are envisioning?
Quoted 22 lines
I think he envisions something like this:
struct __packed cookie_header { u32 magic; u32 length; };Quoted 15 lines
but IHMO, we actually don't need a struct - as all that we would need is to make sure the signing cookie is part of the policy binary and leave the rest of the error handling to ASP/TEE modules (we can rely on the feedback from those modules).
Quoted 2 lines
Shyam, do you think a struct makes sense here? There's some header in this policy, right?
Yes, the policy binary on a whole has multiple sections within it and there are multiple headers (like signing, OEM header, etc).
But that might be not real interest to the PMF driver. The only thing the driver has to make sure is that the policy binary going into ASP (AMD Secure Processor) is with in the limits and has a valid signing cookie. So this part is already taken care in the current code.
Clearly the PMF driver is interested in the header which contains the cookie and length fields as proven by the code (even if that's for the validation purposes only). I'm not asking about add various other headers which are of no interest.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
There are more thing to address here...
1)
amd_pmf_start_policy_engine()function returns-EINVAL& res that is TA_PMF_* which inconsistent in type of the return valueah! it has mix of both int and u32 :-)
I was talking more on a logical level not so much about C type itself.
It is confusing to return error in two ways: as -Exx values and TA_PMF_* which are positive. As a general rule (IMO), the HW specific errors should be mapped to -Exx codes at latest when a function returns also -Exx code which is the case here.
Signature · 1 line
i.
Quoted 14 lines
Quoted 2 lines
Armin, would you like to amend this in your current series? or else I will submit a change for this in my next series.
I can do so, but i will be unable to send a new patch series for the rest of this week.
Quoted 7 lines
Quoted 7 lines
2) Once 1) is fixed, the caller shadowing the return code can be fixed as
well: ret = amd_pmf_start_policy_engine(dev); if (ret) return -EINVAL;