Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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")
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Closes: https://lore.kernel.org/all/aDVyO8ByVsceybk9@stanley.mountain/
Reviewed-by: Naveen Krishna Chatradhi <naveenkrishna.chatradhi@amd.com>
Signed-off-by: Akshay Gupta <akshay.gupta@amd.com>
Link: https://lore.kernel.org/r/20250716110729.2193725-2-akshay.gupta@amd.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Akshay Gupta and committed by
Greg Kroah-Hartman
bbb40139 e108b0a5

+11 -4
+11 -4
drivers/misc/amd-sbi/rmi-core.c
··· 372 372 mutex_unlock(&data->lock); 373 373 374 374 if (msg.rflag && !ret) 375 - return copy_to_user(arg, &msg, sizeof(struct apml_reg_xfer_msg)); 375 + if (copy_to_user(arg, &msg, sizeof(struct apml_reg_xfer_msg))) 376 + return -EFAULT; 376 377 return ret; 377 378 } 378 379 ··· 391 390 if (ret && ret != -EPROTOTYPE) 392 391 return ret; 393 392 394 - return copy_to_user(arg, &msg, sizeof(struct apml_mbox_msg)); 393 + if (copy_to_user(arg, &msg, sizeof(struct apml_mbox_msg))) 394 + return -EFAULT; 395 + return ret; 395 396 } 396 397 397 398 static int apml_cpuid_xfer(struct sbrmi_data *data, struct apml_cpuid_msg __user *arg) ··· 410 407 if (ret && ret != -EPROTOTYPE) 411 408 return ret; 412 409 413 - return copy_to_user(arg, &msg, sizeof(struct apml_cpuid_msg)); 410 + if (copy_to_user(arg, &msg, sizeof(struct apml_cpuid_msg))) 411 + return -EFAULT; 412 + return ret; 414 413 } 415 414 416 415 static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __user *arg) ··· 429 424 if (ret && ret != -EPROTOTYPE) 430 425 return ret; 431 426 432 - return copy_to_user(arg, &msg, sizeof(struct apml_mcamsr_msg)); 427 + if (copy_to_user(arg, &msg, sizeof(struct apml_mcamsr_msg))) 428 + return -EFAULT; 429 + return ret; 433 430 } 434 431 435 432 static long sbrmi_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)