misc: amd-sbi: Address potential integer overflow issue reported in smatch
PATCH v3 1/2 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 thread data from input is available at byte 4 & 5, this patch fixes to copy the user data correctly in the argument.
---
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
@@ -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
PATCH v3 2/2 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.
---
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
@@ -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 __useif (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 __userif (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 __usif (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
Greg KH
maintainerRe: [PATCH v3 2/2] misc: amd-sbi: Address copy_to/from_user() warning reported in smatch
Quoted 34 lines · Akshay Gupta
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:376apml_rmi_reg_xfer()warn: maybe return-EFAULTinstead of the bytes remaining?drivers/misc/amd-sbi/rmi-core.c:394apml_mailbox_xfer()warn: maybe return-EFAULTinstead of the bytes remaining?drivers/misc/amd-sbi/rmi-core.c:411apml_cpuid_xfer()warn: maybe return-EFAULTinstead of the bytes remaining?drivers/misc/amd-sbi/rmi-core.c:428apml_mcamsr_xfer()warn: maybe return-EFAULTinstead 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.Reported-by Dan Carpenter <[email protected]>Reviewed-by Naveen Krishna Chatradhi <[email protected]>Signed-off-by Akshay Gupta <[email protected]>---
Changes from v2:
- Update commit message to add, "fix the-EPROTOTYPEerror return".Changes from v1:
- Split patch as per Greg's suggestiondrivers/misc/amd-sbi/rmi-core.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)drivers/misc/amd-sbi/rmi-core.c +0
-0
drivers/misc/amd-sbi/rmi-core.c static int apml_rmi_reg_xfer(struct sbrmi_data *data, · line 377
Quoted 7 lines
drivers/misc/amd-sbi/rmi-core.c +2
-1@@ -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;
What commit id does this change fix?
thanks,
greg k-h
Re: [PATCH v3 2/2] misc: amd-sbi: Address copy_to/from_user() warning reported in smatch
Quoted 40 lines · Greg KH
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 36 lines · Akshay Gupta
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:376apml_rmi_reg_xfer()warn: maybe return-EFAULTinstead of the bytes remaining?drivers/misc/amd-sbi/rmi-core.c:394apml_mailbox_xfer()warn: maybe return-EFAULTinstead of the bytes remaining?drivers/misc/amd-sbi/rmi-core.c:411apml_cpuid_xfer()warn: maybe return-EFAULTinstead of the bytes remaining?drivers/misc/amd-sbi/rmi-core.c:428apml_mcamsr_xfer()warn: maybe return-EFAULTinstead 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.Reported-by Dan Carpenter <[email protected]>Reviewed-by Naveen Krishna Chatradhi <[email protected]>Signed-off-by Akshay Gupta <[email protected]>---
Changes from v2:
- Update commit message to add, "fix the-EPROTOTYPEerror return".Changes from v1:
- Split patch as per Greg's suggestiondrivers/misc/amd-sbi/rmi-core.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)drivers/misc/amd-sbi/rmi-core.c +0
-0@@ -372,7 +372,8 @@ static int apml_rmi_reg_xfer(struct sbrmi_data *data,mutex_unlock (&data->lock);
Quoted 6 lines
drivers/misc/amd-sbi/rmi-core.c static int apml_rmi_reg_xfer(struct sbrmi_data *data, · line 377
Quoted 5 lines
drivers/misc/amd-sbi/rmi-core.c +2
-1if (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;What commit id does this change fix?
I will update the commit message to address this, thank you
Quoted 4 lines
thanks,
greg k-h
Greg KH
maintainerQuoted 38 lines · Akshay Gupta
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:132rmi_cpuid_read()warn: bitwise OR is zero '0xffffffff00000000&0xffff'drivers/misc/amd-sbi/rmi-core.c:132rmi_cpuid_read()warn: potential integer overflow from user 'msg->cpu_in_out<< 32'drivers/misc/amd-sbi/rmi-core.c:213rmi_mca_msr_read()warn: bitwise OR is zero '0xffffffff00000000&0xffff'drivers/misc/amd-sbi/rmi-core.c:213rmi_mca_msr_read()warn: potential integer overflow from user 'msg->mcamsr_in_out<< 32'CPUID thread data from input is available at byte 4 & 5, this patch fixes to copy the user data correctly in the argument.
Reported-by Dan Carpenter <[email protected]>Reviewed-by Naveen Krishna Chatradhi <[email protected]>Signed-off-by Akshay Gupta <[email protected]>---
Changes from v1:
- Split patch as per Greg's suggestiondrivers/misc/amd-sbi/rmi-core.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/misc/amd-sbi/rmi-core.c +0
-1@@ -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
drivers/misc/amd-sbi/rmi-core.c static int rmi_cpuid_read(struct sbrmi_data *data, · line 131
Quoted 5 lines
drivers/misc/amd-sbi/rmi-core.c +1
-1@@ -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 ;
So this takes a u64 and just moves it over 32 bits and then does what? I guess it makes sense but how did the original code ever work at all?
drivers/misc/amd-sbi/rmi-core.c static int rmi_mca_msr_read(struct sbrmi_data *data, · line 212
Quoted 9 lines
drivers/misc/amd-sbi/rmi-core.c +1
-1/* 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 ;
Same here, was the original code just wrong?
And if this wrong, should this get a fixes: line?
thanks,
greg k-h
Quoted 45 lines · Greg KH
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 41 lines · Akshay Gupta
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:132rmi_cpuid_read()warn: bitwise OR is zero '0xffffffff00000000&0xffff'drivers/misc/amd-sbi/rmi-core.c:132rmi_cpuid_read()warn: potential integer overflow from user 'msg->cpu_in_out<< 32'drivers/misc/amd-sbi/rmi-core.c:213rmi_mca_msr_read()warn: bitwise OR is zero '0xffffffff00000000&0xffff'drivers/misc/amd-sbi/rmi-core.c:213rmi_mca_msr_read()warn: potential integer overflow from user 'msg->mcamsr_in_out<< 32'CPUID thread data from input is available at byte 4 & 5, this patch fixes to copy the user data correctly in the argument.
Reported-by Dan Carpenter <[email protected]>Reviewed-by Naveen Krishna Chatradhi <[email protected]>Signed-off-by Akshay Gupta <[email protected]>---
Changes from v1:
- Split patch as per Greg's suggestiondrivers/misc/amd-sbi/rmi-core.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/misc/amd-sbi/rmi-core.c +0
-1@@ -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; }
Quoted 4 lines
drivers/misc/amd-sbi/rmi-core.c static int rmi_cpuid_read(struct sbrmi_data *data, · line 131
Quoted 2 lines
drivers/misc/amd-sbi/rmi-core.c +1
-1- thread = msg->cpu_in_out <<CPUID_MCA_THRD_INDEX &CPUID_MCA_THRD_MASK ; + thread = msg->cpu_in_out >>CPUID_MCA_THRD_INDEX ;So this takes a u64 and just moves it over 32 bits and then does what? I guess it makes sense but how did the original code ever work at all?
CPUID register data(leaf and sub-leaf) is provided to user for the input thread.
Previously data is return only for thread 0 and not for other threads.
Quoted 11 lines
drivers/misc/amd-sbi/rmi-core.c static int rmi_mca_msr_read(struct sbrmi_data *data, · line 212
Quoted 8 lines
drivers/misc/amd-sbi/rmi-core.c +1
-1/* 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 ;Same here, was the original code just wrong?
And if this wrong, should this get a fixes: line?
I will update the commit message to include the commit ID and the line.
Quoted 4 lines
thanks,
greg k-h