Skip to contentSkip to conversation

misc: amd-sbi: Address potential integer overflow issue reported in smatch

PATCH New Akshay Gupta 1 participant 3 messages Jul 16, 2025 Char and misc drivers

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/3 misc: amd-sbi: Address potential integer overflow issue reported in smatch

Smatch warnings are reported for below commit,

Commit bb13a84ed6b7 ("misc: amd-sbi: Add support for CPUID protocol") from Apr 28, 2025 (linux-next), leads to the following Smatch static checker warning:

drivers/misc/amd-sbi/rmi-core.c:132 rmi_cpuid_read() warn: bitwise OR is zero '0xffffffff00000000 & 0xffff' drivers/misc/amd-sbi/rmi-core.c:132 rmi_cpuid_read() warn: potential integer overflow from user 'msg->cpu_in_out << 32' drivers/misc/amd-sbi/rmi-core.c:213 rmi_mca_msr_read() warn: bitwise OR is zero '0xffffffff00000000 & 0xffff' drivers/misc/amd-sbi/rmi-core.c:213 rmi_mca_msr_read() warn: potential integer overflow from user 'msg->mcamsr_in_out << 32'

CPUID & MCAMSR thread data from input is available at byte 4 & 5, this patch fixes to copy the user data correctly in the argument. Previously, CPUID and MCAMSR data is return only for thread 0.

Fixes bb13a84ed6b7 ("misc: amd-sbi: Add support for CPUID protocol")
Fixes 69b1ba83d21c ("misc: amd-sbi: Add support for read MCA register protocol")

---
Changes from v3:
- Update commit message to address Greg's comment to include "Fixes: Commit ID"
- Update commit message to include MCAMSR thread data index fix.

Changes from v1:
- Split patch as per Greg's suggestion

 drivers/misc/amd-sbi/rmi-core.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/misc/amd-sbi/rmi-core.c +2 -3
index b653a21a909e..3570f3b269a9 100644
@@ -42,7 +42,6 @@
 #define RD_MCA_CMD	0x86
 
 /* CPUID MCAMSR mask & index */
-#define CPUID_MCA_THRD_MASK	GENMASK(15, 0)
 #define CPUID_MCA_THRD_INDEX	32
 #define CPUID_MCA_FUNC_MASK	GENMASK(31, 0)
 #define CPUID_EXT_FUNC_INDEX	56
@@ -129,7 +128,7 @@ static int rmi_cpuid_read(struct sbrmi_data *data,
 		goto exit_unlock;
 	}
 
-	thread = msg->cpu_in_out << CPUID_MCA_THRD_INDEX & CPUID_MCA_THRD_MASK;
+	thread = msg->cpu_in_out >> CPUID_MCA_THRD_INDEX;
 
 	/* Thread > 127, Thread128 CS register, 1'b1 needs to be set to 1 */
 	if (thread > 127) {
@@ -210,7 +209,7 @@ static int rmi_mca_msr_read(struct sbrmi_data *data,
 		goto exit_unlock;
 	}
 
-	thread = msg->mcamsr_in_out << CPUID_MCA_THRD_INDEX & CPUID_MCA_THRD_MASK;
+	thread = msg->mcamsr_in_out >> CPUID_MCA_THRD_INDEX;
 
 	/* Thread > 127, Thread128 CS register, 1'b1 needs to be set to 1 */
 	if (thread > 127) {
Signature · 1 line
2.25.1
#1 lore raw

PATCH v5 2/3 misc: amd-sbi: Address copy_to/from_user() warning reported in smatch

Smatch warnings are reported for below commit,

Commit bb13a84ed6b7 ("misc: amd-sbi: Add support for CPUID protocol") from Apr 28, 2025 (linux-next), leads to the following Smatch static checker warning:

drivers/misc/amd-sbi/rmi-core.c:376 apml_rmi_reg_xfer() warn: maybe return -EFAULT instead of the bytes remaining? drivers/misc/amd-sbi/rmi-core.c:394 apml_mailbox_xfer() warn: maybe return -EFAULT instead of the bytes remaining? drivers/misc/amd-sbi/rmi-core.c:411 apml_cpuid_xfer() warn: maybe return -EFAULT instead of the bytes remaining? drivers/misc/amd-sbi/rmi-core.c:428 apml_mcamsr_xfer() warn: maybe return -EFAULT instead of the bytes remaining?

copy_to/from_user() returns number of bytes, not copied. In case data not copied, return "-EFAULT". Additionally, fixes the "-EPROTOTYPE" error return as intended.

Fixes 35ac2034db72 ("misc: amd-sbi: Add support for AMD_SBI IOCTL")
Fixes bb13a84ed6b7 ("misc: amd-sbi: Add support for CPUID protocol")
Fixes 69b1ba83d21c ("misc: amd-sbi: Add support for read MCA register protocol")
Fixes cf141287b774 ("misc: amd-sbi: Add support for register xfer")

---
Changes from v3:
- Update commit message to include "Fixes: Commit ID" as Greg's suggestion

Changes from v2:
- Update commit message to add, "fix the -EPROTOTYPE error return".

Changes from v1:
- Split patch as per Greg's suggestion

 drivers/misc/amd-sbi/rmi-core.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/misc/amd-sbi/rmi-core.c +11 -4
index 3570f3b269a9..9048517c088c 100644
@@ -372,7 +372,8 @@ static int apml_rmi_reg_xfer(struct sbrmi_data *data,
 	mutex_unlock(&data->lock);
 
 	if (msg.rflag && !ret)
-		return copy_to_user(arg, &msg, sizeof(struct apml_reg_xfer_msg));
+		if (copy_to_user(arg, &msg, sizeof(struct apml_reg_xfer_msg)))
+			return -EFAULT;
 	return ret;
 }
 
@@ -390,7 +391,9 @@ static int apml_mailbox_xfer(struct sbrmi_data *data, struct apml_mbox_msg __use
 	if (ret && ret != -EPROTOTYPE)
 		return ret;
 
-	return copy_to_user(arg, &msg, sizeof(struct apml_mbox_msg));
+	if (copy_to_user(arg, &msg, sizeof(struct apml_mbox_msg)))
+		return -EFAULT;
+	return ret;
 }
 
 static int apml_cpuid_xfer(struct sbrmi_data *data, struct apml_cpuid_msg __user *arg)
@@ -407,7 +410,9 @@ static int apml_cpuid_xfer(struct sbrmi_data *data, struct apml_cpuid_msg __user
 	if (ret && ret != -EPROTOTYPE)
 		return ret;
 
-	return copy_to_user(arg, &msg, sizeof(struct apml_cpuid_msg));
+	if (copy_to_user(arg, &msg, sizeof(struct apml_cpuid_msg)))
+		return -EFAULT;
+	return ret;
 }
 
 static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __user *arg)
@@ -424,7 +429,9 @@ static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __us
 	if (ret && ret != -EPROTOTYPE)
 		return ret;
 
-	return copy_to_user(arg, &msg, sizeof(struct apml_mcamsr_msg));
+	if (copy_to_user(arg, &msg, sizeof(struct apml_mcamsr_msg)))
+		return -EFAULT;
+	return ret;
 }
 
 static long sbrmi_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
Signature · 1 line
2.25.1
#2 lore raw

PATCH v5 3/3 misc: amd-sbi: Explicitly clear in/out arg "mb_in_out"

In reply to Akshay Gupta

- New AMD processor will support different input/output for same command.
- In some scenarios the input value is not cleared, which will be added to output before reporting the data.
- Clearing input explicitly will be a cleaner and safer approach.

---
New patch

 drivers/misc/amd-sbi/rmi-core.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/misc/amd-sbi/rmi-core.c +4 -0
index 9048517c088c..3dec2fc00124 100644
@@ -320,6 +320,10 @@ int rmi_mailbox_xfer(struct sbrmi_data *data,
 	ret = regmap_read(data->regmap, SBRMI_OUTBNDMSG7, &ec);
 	if (ret || ec)
 		goto exit_clear_alert;
+
+	/* Clear the input value before updating the output data */
+	msg->mb_in_out = 0;
+
 	/*
 	 * For a read operation, the initiator (BMC) reads the firmware
 	 * response Command Data Out[31:0] from SBRMI::OutBndMsg_inst[4:1]
Signature · 1 line
2.25.1