From: Jeff Law <jeffreyalaw@gmail.com>
To: Kees Cook <kees@kernel.org>, Qing Zhao <qing.zhao@oracle.com>
Cc: Uros Bizjak <ubizjak@gmail.com>,
Joseph Myers <josmyers@redhat.com>,
Richard Biener <rguenther@suse.de>,
Andrew Pinski <pinskia@gmail.com>,
Jakub Jelinek <jakub@redhat.com>,
Martin Uecker <uecker@tugraz.at>,
Peter Zijlstra <peterz@infradead.org>,
Ard Biesheuvel <ardb@kernel.org>, Jan Hubicka <hubicka@ucw.cz>,
Richard Earnshaw <richard.earnshaw@arm.com>,
Richard Sandiford <richard.sandiford@arm.com>,
Marcus Shawcroft <marcus.shawcroft@arm.com>,
Kyrylo Tkachov <kyrylo.tkachov@arm.com>,
Kito Cheng <kito.cheng@gmail.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Andrew Waterman <andrew@sifive.com>,
Jim Wilson <jim.wilson.gcc@gmail.com>,
Dan Li <ashimida.1990@gmail.com>,
Sami Tolvanen <samitolvanen@google.com>,
Ramon de C Valle <rcvalle@google.com>,
Joao Moreira <joao@overdrivepizza.com>,
Nathan Chancellor <nathan@kernel.org>,
Bill Wendling <morbo@google.com>,
"Osterlund, Sebastian" <sebastian.osterlund@intel.com>,
"Constable, Scott D" <scott.d.constable@intel.com>,
gcc-patches@gcc.gnu.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH v7 7/7] riscv: Add RISC-V Kernel Control Flow Integrity implementation
Date: Wed, 19 Nov 2025 08:35:35 -0700 [thread overview]
Message-ID: <c8b5038c-222f-4215-84c4-3f0698ccf10e@gmail.com> (raw)
In-Reply-To: <20251117201231.2160039-7-kees@kernel.org>
On 11/17/25 1:12 PM, Kees Cook wrote:
> Implement RISC-V-specific KCFI backend. Nothing is conceptually rv64
> specific, but using an alternative set of instructions for rv32 would be
> needed, and at present the only user of KCFI on riscv is the rv64 build
> of the Linux kernel.
>
> - Scratch register allocation using t1/t2 (x6/x7) following RISC-V
> procedure call standard for temporary registers (already
> caller-saved), and t3 (x8) when either t1 or t2 is already the call
> target register.
>
> - Incompatible with -ffixed-t1, -ffixed-t2, or -ffixed-t3.
>
> - Integration with .kcfi_traps section for debugger/runtime metadata
> (like x86_64).
>
> Assembly Code Pattern for RISC-V:
> lw t1, -4(target_reg) ; Load actual type ID from preamble
> lui t2, %hi(expected_type) ; Load expected type (upper 20 bits)
> addiw t2, t2, %lo(expected_type) ; Add lower 12 bits (sign-extended)
> beq t1, t2, .Lkcfi_call ; Branch if types match
> .Lkcfi_trap: ebreak ; Environment break trap on mismatch
> .Lkcfi_call: jalr/jr target_reg ; Execute validated indirect transfer
>
> Build and run tested with Linux kernel ARCH=riscv.
>
> gcc/ChangeLog:
>
> config/riscv/riscv-protos.h: Declare KCFI helpers.
> config/riscv/riscv.cc (riscv_maybe_wrap_call_with_kcfi): New
> function, to wrap calls.
> (riscv_maybe_wrap_call_value_with_kcfi): New function, to
> wrap calls with return values.
> (riscv_output_kcfi_insn): New function to emit KCFI assembly.
> config/riscv/riscv.md: Add KCFI RTL patterns and hook expansion.
> doc/invoke.texi: Document riscv nuances.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/kcfi/kcfi-adjacency.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-basics.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-call-sharing.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-complex-addressing.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-move-preservation.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-no-sanitize-inline.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-no-sanitize.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-offset-validation.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-patchable-entry-only.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-patchable-large.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-patchable-medium.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-patchable-prefix-only.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-tail-calls.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-trap-section.c: Add riscv patterns.
> * gcc.dg/kcfi/kcfi-riscv-fixed-t1.c: New test.
> * gcc.dg/kcfi/kcfi-riscv-fixed-t2.c: New test.
> * gcc.dg/kcfi/kcfi-riscv-fixed-t3.c: New test.
Note there's still review work to do on the RISC-V bits. But it's worth
noting your patch triggers ICEs in the RISC-V testsuite. See:
https://github.com/ewlu/gcc-precommit-ci/issues/4115#issuecomment-3543758441
Jeff
next prev parent reply other threads:[~2025-11-19 15:35 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-17 20:12 [PATCH v7 0/7] Introduce Kernel Control Flow Integrity ABI [PR107048] Kees Cook
2025-11-17 20:12 ` [PATCH v7 1/7] typeinfo: Introduce KCFI typeinfo mangling API Kees Cook
2025-11-17 20:12 ` [PATCH v7 2/7] kcfi: Add core Kernel Control Flow Integrity infrastructure Kees Cook
2025-11-17 20:12 ` [PATCH v7 3/7] kcfi: Add regression test suite Kees Cook
2025-11-17 20:12 ` [PATCH v7 4/7] x86: Add x86_64 Kernel Control Flow Integrity implementation Kees Cook
2025-11-17 20:12 ` [PATCH v7 5/7] aarch64: Add AArch64 " Kees Cook
2025-11-17 20:12 ` [PATCH v7 6/7] arm: Add ARM 32-bit " Kees Cook
2025-11-17 20:12 ` [PATCH v7 7/7] riscv: Add RISC-V " Kees Cook
2025-11-19 15:35 ` Jeff Law [this message]
2025-11-20 0:40 ` Kees Cook
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c8b5038c-222f-4215-84c4-3f0698ccf10e@gmail.com \
--to=jeffreyalaw@gmail.com \
--cc=andrew@sifive.com \
--cc=ardb@kernel.org \
--cc=ashimida.1990@gmail.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=hubicka@ucw.cz \
--cc=jakub@redhat.com \
--cc=jim.wilson.gcc@gmail.com \
--cc=joao@overdrivepizza.com \
--cc=josmyers@redhat.com \
--cc=kees@kernel.org \
--cc=kito.cheng@gmail.com \
--cc=kyrylo.tkachov@arm.com \
--cc=linux-hardening@vger.kernel.org \
--cc=marcus.shawcroft@arm.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=palmer@dabbelt.com \
--cc=peterz@infradead.org \
--cc=pinskia@gmail.com \
--cc=qing.zhao@oracle.com \
--cc=rcvalle@google.com \
--cc=rguenther@suse.de \
--cc=richard.earnshaw@arm.com \
--cc=richard.sandiford@arm.com \
--cc=samitolvanen@google.com \
--cc=scott.d.constable@intel.com \
--cc=sebastian.osterlund@intel.com \
--cc=ubizjak@gmail.com \
--cc=uecker@tugraz.at \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox