platform/x86/amd/pmf: Fix policy binary handling
Subsystems (2)
v3 cover platform/x86/amd/pmf: Fix policy binary handling
This patch series fixes various issues inside the policy binary handling code.
The first patch makes sure that a valid error code is returned upon failing to start the policy engine, while the second patch drops the usage of readl() on non-io memory.
The last two patches fix a possible out-of-bounds memory access when parsing the policy binary header.
All patches are compile-tested only.
Changes since v2:
- add patches 1 and 3
Changes since v1:
- get the full dword instead of only 8 bits when reading the header
- check if the policy buffer also has enough room for storing the length
Armin Wolf (4):
platform/x86/amd/pmf: Fix return value of
amd_pmf_start_policy_engine()
platform/x86/amd/pmf: Do not use readl() for policy buffer access platform/x86/amd/pmf: Use struct for cookie header platform/x86/amd/pmf: Fix possible out-of-bound memory accesses
drivers/platform/x86/amd/pmf/pmf.h | 6 +++++- drivers/platform/x86/amd/pmf/tee-if.c | 21 +++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-)
--
2.39.2
PATCH v3 1/4 platform/x86/amd/pmf: Fix return value of amd_pmf_start_policy_engine()
amd_pmf_start_policy_engine() returns an negative error code upon failure, so the TA_PMF_* error codes cannot be used here.
Return -EIO instead. Also stop shadowing the return code in amd_pmf_get_pb_data().
Compile-tested only.
--- drivers/platform/x86/amd/pmf/tee-if.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/platform/x86/amd/pmf/tee-if.c +3 -3
@@ -269,7 +269,7 @@ static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev) }else {dev_err (dev->dev,"ta invoke cmd init failed err: %x\n" , res); dev->smart_pc_enabled =PMF_SMART_PC_DISABLED ; -return res; +return -EIO ; }return 0 ;
@@ -309,8 +309,8 @@ static ssize_t amd_pmf_get_pb_data(struct file *filp, const char __user *buf,amd_pmf_hex_dump_pb (dev); ret =amd_pmf_start_policy_engine (dev); -if (ret) -return -EINVAL ; +if (ret <0 ) +return ret;return length; }
--
2.39.2
PATCH v3 2/4 platform/x86/amd/pmf: Do not use readl() for policy buffer access
In reply to Armin WolfThe policy buffer is allocated using normal memory allocation functions, so readl() should not be used on it.
Compile-tested only.
--- 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 v3 3/4 platform/x86/amd/pmf: Use struct for cookie header
In reply to Armin WolfThe cookie header consists of a sign field and a length field. Combine both in a single struct to make accesses simpler.
Compile-tested only.
--- drivers/platform/x86/amd/pmf/pmf.h | 6 +++++- drivers/platform/x86/amd/pmf/tee-if.c | 9 ++++----- 2 files changed, 9 insertions(+), 6 deletions(-)
drivers/platform/x86/amd/pmf/pmf.h +5 -1
@@ -17,7 +17,11 @@#define POLICY_BUF_MAX_SZ 0x4b000#define POLICY_SIGN_COOKIE 0x31535024#define POLICY_COOKIE_OFFSET 0x10 -#define POLICY_COOKIE_LEN 0x14 + +struct cookie_header { +u32 sign; +u32 length; +} __packed;/* APMF Functions */ #define APMF_FUNC_VERIFY_INTERFACE 0
drivers/platform/x86/amd/pmf/tee-if.c +4 -5
@@ -246,17 +246,16 @@ static void amd_pmf_invoke_cmd(struct work_struct *work)static int amd_pmf_start_policy_engine (struct amd_pmf_dev *dev) { -u32 cookie, length; +struct cookie_header *header;int res; - cookie = *(u32 *)(dev->policy_buf +POLICY_COOKIE_OFFSET ); - length = *(u32 *)(dev->policy_buf +POLICY_COOKIE_LEN ); + header = (struct cookie_header *)(dev->policy_buf +POLICY_COOKIE_OFFSET ); -if (cookie !=POLICY_SIGN_COOKIE || !length) +if (header->sign !=POLICY_SIGN_COOKIE || !header->length)return -EINVAL ;/* Update the actual length */ - dev->policy_sz = length +512 ; + dev->policy_sz = header->length +512 ; res =amd_pmf_invoke_cmd_init (dev);if (res ==TA_PMF_TYPE_SUCCESS ) {/* Now its safe to announce that smart pc is enabled */
--
2.39.2
PATCH v3 4/4 platform/x86/amd/pmf: Fix possible out-of-bound memory accesses
In reply to Armin WolfThe 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.
--- 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,11 +249,17 @@ static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev)struct cookie_header *header;int res; +if (dev->policy_sz <POLICY_COOKIE_OFFSET +sizeof (*header)) +return -EINVAL ; + header = (struct cookie_header *)(dev->policy_buf +POLICY_COOKIE_OFFSET );if (header->sign !=POLICY_SIGN_COOKIE || !header->length)return -EINVAL ; +if (dev->policy_sz < header->length +512 ) +return -EINVAL ; +/* Update the actual length */ dev->policy_sz = header->length +512 ; res =amd_pmf_invoke_cmd_init (dev);
--
2.39.2
Quoted 8 lines · Armin Wolf
This patch series fixes various issues inside the policy binary handling code.
The first patch makes sure that a valid error code is returned upon failing to start the policy engine, while the second patch drops the usage ofreadl()on non-io memory.
The last two patches fix a possible out-of-bounds memory access when parsing the policy binary header.
Quoted 8 lines
All patches are compile-tested only.
Changes since v2:
- add patches 1 and 3Changes since v1:
- get the full dword instead of only 8 bits when reading the header
- check if the policy buffer also has enough room for storing the length
Thank you Armin. Series looks good to me.
Quoted 15 lines
Armin Wolf (4):
platform/x86/amd/pmf: Fix return value of
amd_pmf_start_policy_engine()
platform/x86/amd/pmf: Do not usereadl()for policy buffer access platform/x86/amd/pmf: Use struct for cookie header platform/x86/amd/pmf: Fix possible out-of-bound memory accessesdrivers/platform/x86/amd/pmf/pmf.h | 6 +++++- drivers/platform/x86/amd/pmf/tee-if.c | 21 +++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-)--
2.39.2
Quoted 14 lines · Shyam Sundar S K
Quoted 13 lines · Armin Wolf
This patch series fixes various issues inside the policy binary handling code.
The first patch makes sure that a valid error code is returned upon failing to start the policy engine, while the second patch drops the usage ofreadl()on non-io memory.
The last two patches fix a possible out-of-bounds memory access when parsing the policy binary header.All patches are compile-tested only.
Changes since v2:
- add patches 1 and 3
Quoted 7 lines
Quoted 3 lines
Changes since v1:
- get the full dword instead of only 8 bits when reading the header
- check if the policy buffer also has enough room for storing the lengthThank you Armin. Series looks good to me.
Reviewed-by Shyam Sundar S K [email protected]
Thanks for taking a look.
Btw, you were missing <> around the address so the patchwork automation didn't catch that line. To help the patchwork to capture that tag (no need for you to do anything):
--
i.
Quoted 6 lines
Quoted 6 lines
Armin Wolf (4):
platform/x86/amd/pmf: Fix return value of
amd_pmf_start_policy_engine()
platform/x86/amd/pmf: Do not usereadl()for policy buffer access platform/x86/amd/pmf: Use struct for cookie header platform/x86/amd/pmf: Fix possible out-of-bound memory accesses
Ilpo Järvinen
maintainerQuoted 9 lines · Armin Wolf
This patch series fixes various issues inside the policy binary handling code.
The first patch makes sure that a valid error code is returned upon failing to start the policy engine, while the second patch drops the usage ofreadl()on non-io memory.
The last two patches fix a possible out-of-bounds memory access when parsing the policy binary header.[...]
Thank you for your contribution, it has been applied to my local review-ilpo branch. Note it will show up in the public platform-drivers-x86/review-ilpo branch only once I've pushed my local branch there, which might take a while.
The list of commits applied:
[1/4] platform/x86/amd/pmf: Fix return value of amd_pmf_start_policy_engine()
commit: 98cfcece0ab86c99bc106633d764fb6ad4a35b8e
[2/4] platform/x86/amd/pmf: Do not use readl() for policy buffer access
commit: 379a7c64c4fa33315b504ede86a87188dc88fef4
[3/4] platform/x86/amd/pmf: Use struct for cookie header
commit: a87d92223084f61d37da4952ad68634ea8a7caaf
[4/4] platform/x86/amd/pmf: Fix possible out-of-bound memory accesses
commit: 1e7a14ee259e2ff85be51bf36a7692b20233159a--
i.