[PATCH] riscv: fix incorrect use of __user pointer

From: Clément Léger
Date: Wed Nov 22 2023 - 08:52:07 EST


These warnings were reported by sparse and were due to lack of __user
annotation as well as dereferencing such pointer. Fix this by adding
__user cast were needed (load_u8()/store_u8()) and move the __user
annotation in __read_insn() into the if() branch that uses __get_user()
with this pointer.

Reported-by: kernel test robot <lkp@xxxxxxxxx>
Closes: https://lore.kernel.org/oe-kbuild-all/202311160606.obGOOwB3-lkp@xxxxxxxxx/
Signed-off-by: Clément Léger <cleger@xxxxxxxxxxxx>
---
arch/riscv/kernel/traps_misaligned.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c
index 5eba37147caa..02abb6616873 100644
--- a/arch/riscv/kernel/traps_misaligned.c
+++ b/arch/riscv/kernel/traps_misaligned.c
@@ -319,7 +319,7 @@ static inline int get_insn(struct pt_regs *regs, ulong mepc, ulong *r_insn)
static inline int load_u8(struct pt_regs *regs, const u8 *addr, u8 *r_val)
{
if (user_mode(regs)) {
- return __get_user(*r_val, addr);
+ return __get_user(*r_val, (u8 __user *)addr);
} else {
*r_val = *addr;
return 0;
@@ -329,7 +329,7 @@ static inline int load_u8(struct pt_regs *regs, const u8 *addr, u8 *r_val)
static inline int store_u8(struct pt_regs *regs, u8 *addr, u8 val)
{
if (user_mode(regs)) {
- return __put_user(val, addr);
+ return __put_user(val, (u8 __user *)addr);
} else {
*addr = val;
return 0;
@@ -341,7 +341,7 @@ static inline int store_u8(struct pt_regs *regs, u8 *addr, u8 val)
int __ret; \
\
if (user_mode(regs)) { \
- __ret = __get_user(insn, insn_addr); \
+ __ret = __get_user(insn, (typeof(*insn_addr) __user *) insn_addr); \
} else { \
insn = *insn_addr; \
__ret = 0; \
@@ -356,7 +356,7 @@ static inline int get_insn(struct pt_regs *regs, ulong epc, ulong *r_insn)

if (epc & 0x2) {
ulong tmp = 0;
- u16 __user *insn_addr = (u16 __user *)epc;
+ u16 *insn_addr = (u16 *)epc;

if (__read_insn(regs, insn, insn_addr))
return -EFAULT;
@@ -376,7 +376,7 @@ static inline int get_insn(struct pt_regs *regs, ulong epc, ulong *r_insn)

return 0;
} else {
- u32 __user *insn_addr = (u32 __user *)epc;
+ u32 *insn_addr = (u32 *)epc;

if (__read_insn(regs, insn, insn_addr))
return -EFAULT;
--
2.42.0