Skip to contentSkip to conversation

platform/x86/amd/pmf: Do not use readl() for policy buffer access

PATCH Superseded Armin Wolf 2 participants 3 messages Feb 27, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Armin Wolf

author
#0 lore raw

PATCH 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.

---
 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
index 16973bebf55f..70d09103ab18 100644
@@ -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 = dev->policy_buf[POLICY_COOKIE_OFFSET];
+	length = dev->policy_buf[POLICY_COOKIE_LEN];
 
 	if (cookie != POLICY_SIGN_COOKIE || !length)
 		return -EINVAL;

--
2.39.2

author
#1 lore raw

PATCH 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.

---
 drivers/platform/x86/amd/pmf/tee-if.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/platform/x86/amd/pmf/tee-if.c +6 -0
index 70d09103ab18..f2f9204b3a11 100644
@@ -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)
+		return -EINVAL;
+
 	cookie = dev->policy_buf[POLICY_COOKIE_OFFSET];
 	length = 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

maintainer
#2 lore raw
In reply to Armin Wolf
Quoted 15 lines · Armin Wolf

The 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 +0 -0
index 16973bebf55f..70d09103ab18 100644

drivers/platform/x86/amd/pmf/tee-if.c static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev) · line 253

Quoted 7 lines
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 = dev->policy_buf[POLICY_COOKIE_OFFSET];
+	length = dev->policy_buf[POLICY_COOKIE_LEN];

Hmm, the next question is, is it okay to get just 8 bits instead the full dword (the policy_buf is unsigned char *)?

Signature · 1 line
 i.