linux/arch/riscv
Nathan Chancellor 3f1c81ae13 riscv: Avoid fortify warning in syscall_get_arguments()
commit adf53771a3 upstream.

When building with CONFIG_FORTIFY_SOURCE=y and W=1, there is a warning
because of the memcpy() in syscall_get_arguments():

  In file included from include/linux/string.h:392,
                   from include/linux/bitmap.h:13,
                   from include/linux/cpumask.h:12,
                   from arch/riscv/include/asm/processor.h:55,
                   from include/linux/sched.h:13,
                   from kernel/ptrace.c:13:
  In function 'fortify_memcpy_chk',
      inlined from 'syscall_get_arguments.isra' at arch/riscv/include/asm/syscall.h:66:2:
  include/linux/fortify-string.h:580:25: error: call to '__read_overflow2_field' declared with attribute warning: detected read beyond size of field (2nd parameter); maybe use struct_group()? [-Werror=attribute-warning]
    580 |                         __read_overflow2_field(q_size_field, size);
        |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  cc1: all warnings being treated as errors

The fortified memcpy() routine enforces that the source is not overread
and the destination is not overwritten if the size of either field and
the size of the copy are known at compile time. The memcpy() in
syscall_get_arguments() intentionally overreads from a1 to a5 in
'struct pt_regs' but this is bigger than the size of a1.

Normally, this could be solved by wrapping a1 through a5 with
struct_group() but there was already a struct_group() applied to these
members in commit bba547810c ("riscv: tracing: Fix
__write_overflow_field in ftrace_partial_regs()").

Just avoid memcpy() altogether and write the copying of args from regs
manually, which clears up the warning at the expense of three extra
lines of code.

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Reviewed-by: Dmitry V. Levin <ldv@strace.io>
Fixes: e2c0cdfba7 ("RISC-V: User-facing API")
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20250409-riscv-avoid-fortify-warning-syscall_get_arguments-v1-1-7853436d4755@kernel.org
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
History 2025-04-25 10:47:54 +02:00
..
boot riscv: dts: starfive: Fix a typo in StarFive JH7110 pin function definitions 2025-03-28 22:03:29 +01:00
configs timekeeping: Remove CONFIG_DEBUG_TIMEKEEPING 2024-12-14 20:04:16 +01:00
crypto crypto: riscv - add vector crypto accelerated AES-CBC-CTS 2024-03-20 08:56:11 -07:00
errata RISC-V: errata: Use medany for relocatable builds 2025-04-10 14:39:32 +02:00
include riscv: Avoid fortify warning in syscall_get_arguments() 2025-04-25 10:47:54 +02:00
kernel riscv: KGDB: Remove ".option norvc/.option rvc" for kgdb_compiled_break 2025-04-25 10:47:48 +02:00
kvm RISC-V: KVM: Disable the kernel perf counter during configure 2025-04-10 14:39:11 +02:00
lib Merge patch series "riscv: Improve KASAN coverage to fix unit tests" 2024-09-19 01:10:44 -07:00
mm riscv: Fix hugetlb retrieval of number of ptes in case of !present pte 2025-04-10 14:39:32 +02:00
net riscv, bpf: Make BPF_CMPXCHG fully ordered 2024-10-17 17:14:48 +02:00
purgatory riscv/purgatory: 4B align purgatory_start 2025-04-10 14:39:33 +02:00
tools riscv: Check relocations at compile time 2023-04-19 07:46:32 -07:00
Kbuild RISC-V: hook new crypto subdir into build-system 2024-01-22 17:55:17 -08:00
Kconfig crash, powerpc: default to CRASH_DUMP=n on PPC_BOOK3S_32 2024-11-14 22:43:48 -08:00
Kconfig.debug riscv: Add tests for riscv module loading 2023-11-07 14:59:32 -08:00
Kconfig.errata riscv: thead: Rename T-Head PBMT to MAE 2024-04-25 10:22:33 -07:00
Kconfig.socs RISC-V: drop SOC_VIRT for ARCH_VIRT 2024-04-10 11:37:50 +01:00
Kconfig.vendor riscv: Extend cpufeature.c to detect vendor extensions 2024-07-22 15:36:54 -07:00
Makefile riscv: boot: add Image.xz support 2024-09-01 20:43:27 -07:00
Makefile.postlink kbuild: remove ARCH_POSTLINK from module builds 2023-10-28 21:10:08 +09:00