linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff Law <jeffreyalaw@gmail.com>
To: Kees Cook <kees@kernel.org>, Qing Zhao <qing.zhao@oracle.com>
Cc: Andrew Pinski <pinskia@gmail.com>,
	Richard Biener <rguenther@suse.de>,
	Joseph Myers <josmyers@redhat.com>, 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>,
	Peter Zijlstra <peterz@infradead.org>,
	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>,
	gcc-patches@gcc.gnu.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH v2 6/7] riscv: Add RISC-V Kernel Control Flow Integrity implementation
Date: Mon, 15 Sep 2025 21:40:53 -0600	[thread overview]
Message-ID: <dbf9a593-e19d-4f53-96d0-d067868f40b5@gmail.com> (raw)
In-Reply-To: <20250905002418.464643-6-kees@kernel.org>



On 9/4/25 18:24, Kees Cook wrote:
> Implement RISC-V-specific KCFI backend.
> 
> - Function preamble generation using .word directives for type ID storage
>    at offset from function entry point (no alignment NOPs needed due to
>    fix 4-byte instruction size).
> 
> - Scratch register allocation using t1/t2 (x6/x7) following RISC-V
>    procedure call standard for temporary registers.
> 
> - 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.
> 
> Signed-off-by: Kees Cook <kees@kernel.org>
> ---
>   gcc/config/riscv/riscv-protos.h |   3 +
>   gcc/config/riscv/riscv.cc       | 147 ++++++++++++++++++++++++++++++++
>   gcc/config/riscv/riscv.md       |  74 ++++++++++++++--
>   gcc/doc/invoke.texi             |  13 +++
>   4 files changed, 231 insertions(+), 6 deletions(-)
> 
> diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h
> index 2d60a0ad44b3..0e916fbdde13 100644
> --- a/gcc/config/riscv/riscv-protos.h
> +++ b/gcc/config/riscv/riscv-protos.h
> @@ -126,6 +126,9 @@ extern bool riscv_split_64bit_move_p (rtx, rtx);
>   extern void riscv_split_doubleword_move (rtx, rtx);
>   extern const char *riscv_output_move (rtx, rtx);
>   extern const char *riscv_output_return ();
> +extern rtx riscv_maybe_wrap_call_with_kcfi (rtx, rtx);
> +extern rtx riscv_maybe_wrap_call_value_with_kcfi (rtx, rtx);
> +extern const char *riscv_output_kcfi_insn (rtx_insn *, rtx *);
>   extern void riscv_declare_function_name (FILE *, const char *, tree);
>   extern void riscv_declare_function_size (FILE *, const char *, tree);
>   extern void riscv_asm_output_alias (FILE *, const tree, const tree);

> @@ -11346,6 +11347,149 @@ riscv_convert_vector_chunks (struct gcc_options *opts)
>       return 1;
>   }
>   
> +/* Apply KCFI wrapping to call pattern if needed.  */
> +rtx
> +riscv_maybe_wrap_call_with_kcfi (rtx pat, rtx addr)
So our coding standards require a bit more for that function comment. 
What are PAT and ADDR and how are they used?



> +}
> +
> +/* Apply KCFI wrapping to call_value pattern if needed.  */
> +rtx
> +riscv_maybe_wrap_call_value_with_kcfi (rtx pat, rtx addr)
Similarly here.


> +
> +/* Output the assembly for a KCFI checked call instruction.  */
> +const char *
> +riscv_output_kcfi_insn (rtx_insn *insn, rtx *operands)
And here.


> +{
> +  /* Target register.  */
> +  rtx target_reg = operands[0];
> +  gcc_assert (REG_P (target_reg));
> +
> +  /* Get KCFI type ID.  */
> +  uint32_t expected_type = (uint32_t) INTVAL (operands[3]);
Do we know operands[3] is a CONST_INT?

> +
> +  /* Calculate typeid offset from call target.  */
> +  HOST_WIDE_INT offset = -(4 + kcfi_patchable_entry_prefix_nops);
> +
> +  /* Choose scratch registers that don't conflict with target.  */
> +  unsigned temp1_regnum = T1_REGNUM;
> +  unsigned temp2_regnum = T2_REGNUM;
ISTM that this will need some kidn of adjustment if someone were to 
compile with -ffixed-reg.  Maybe all we really need is a sorry() so that 
if someone were to try to fix the temporary registers they'd get a loud 
complaint from teh compiler.


> +
> +  /* Load actual type from memory at offset.  */
> +  temp_operands[0] = gen_rtx_REG (SImode, temp1_regnum);
> +  temp_operands[1] = gen_rtx_MEM (SImode,
> +                      gen_rtx_PLUS (DImode, target_reg,
> +                                   GEN_INT (offset)));
> +  output_asm_insn ("lw\t%0, %1", temp_operands);
Rather than using DImode for the PLUS, shouldn't it instead use Pmode so 
that it at least tries to work on rv32?  Or is this stuff somehow 
defined as only working for rv64?



> +
> +  /* Execute the indirect call.  */
> +  if (SIBLING_CALL_P (insn))
> +    {
> +      /* Tail call uses x0 (zero register) to avoid saving return address.  */
> +      temp_operands[0] = gen_rtx_REG (DImode, 0);  /* x0 */
> +      temp_operands[1] = target_reg;  /* target register */
> +      temp_operands[2] = const0_rtx;
> +      output_asm_insn ("jalr\t%0, %1, %2", temp_operands);
> +    }
> +  else
> +    {
> +      /* Regular call uses x1 (return address register).  */
> +      temp_operands[0] = gen_rtx_REG (DImode, RETURN_ADDR_REGNUM);  /* x1 */
> +      temp_operands[1] = target_reg;  /* target register */
> +      temp_operands[2] = const0_rtx;
> +      output_asm_insn ("jalr\t%0, %1, %2", temp_operands);
> +    }
More cases where we probably should be using Pmode.

We generally prefer to not generate assembly code like you've done, but 
instead prefer to generate actual RTL.  Is there some reason why you 
decided to use output_asm_insn rather than generating RTL and letting 
usual mechanisms for generating assembly code kick in?


>     rtx target = riscv_legitimize_call_address (XEXP (operands[0], 0));
> -  emit_call_insn (gen_sibcall_internal (target, operands[1], operands[2]));
> +  rtx pat = gen_sibcall_internal (target, operands[1], operands[2]);
> +  pat = riscv_maybe_wrap_call_with_kcfi (pat, target);
> +  emit_call_insn (pat);
>     DONE;
>   })
>   
> +;; KCFI sibling call - matches KCFI wrapper RTL
> +(define_insn "*kcfi_sibcall_insn"
> +  [(kcfi (call (mem:SI (match_operand:DI 0 "call_insn_operand" "l"))
> +               (match_operand 1 ""))
> +         (match_operand 3 "const_int_operand"))
> +   (use (unspec:SI [(match_operand 2 "const_int_operand")] UNSPEC_CALLEE_CC))]
> +  "SIBLING_CALL_P (insn)"
I think the DI for the memory operand should probably be :P instead so 
that we're not so tied to rv64.

> +;; KCFI sibling call with return value - matches KCFI wrapper RTL
> +(define_insn "*kcfi_sibcall_value_insn"
> +  [(set (match_operand 0 "")
> +	(kcfi (call (mem:SI (match_operand:DI 1 "call_insn_operand" "l"))
> +		    (match_operand 2 ""))
> +	      (match_operand 4 "const_int_operand")))
> +   (use (unspec:SI [(match_operand 3 "const_int_operand")] UNSPEC_CALLEE_CC))]
> +  "SIBLING_CALL_P (insn)"
> +{
> +  return riscv_output_kcfi_insn (insn, &operands[1]);
> +}
> +  [(set_attr "type" "call")
> +   (set_attr "length" "24")])
Similarly for this pattern.



>   
> +;; KCFI indirect call - matches KCFI wrapper RTL
> +(define_insn "*kcfi_call_internal"
> +  [(kcfi (call (mem:SI (match_operand:DI 0 "call_insn_operand" "l"))
> +               (match_operand 1 "" ""))
> +         (match_operand 3 "const_int_operand"))
> +   (use (unspec:SI [(match_operand 2 "const_int_operand")] UNSPEC_CALLEE_CC))
> +   (clobber (reg:SI RETURN_ADDR_REGNUM))]
> +  "!SIBLING_CALL_P (insn)"
> +{
> +  return riscv_output_kcfi_insn (insn, operands);
> +}
> +  [(set_attr "type" "call")
> +   (set_attr "length" "24")])
And this one.


>   
> +;; KCFI call with return value - matches KCFI wrapper RTL
> +(define_insn "*kcfi_call_value_insn"
> +  [(set (match_operand 0 "" "")
> +	(kcfi (call (mem:SI (match_operand:DI 1 "call_insn_operand" "l"))
> +		    (match_operand 2 "" ""))
> +	      (match_operand 4 "const_int_operand")))
> +   (use (unspec:SI [(match_operand 3 "const_int_operand")] UNSPEC_CALLEE_CC))
> +   (clobber (reg:SI RETURN_ADDR_REGNUM))]
> +  "!SIBLING_CALL_P (insn)"
> +{
> +  return riscv_output_kcfi_insn (insn, &operands[1]);
> +}
> +  [(set_attr "type" "call")
> +   (set_attr "length" "24")])
THis one too.


64).
>   
> +On RISC-V, KCFI type identifiers are emitted as a @code{.word ID}
> +directive (a 32-bit constant) before the function entry, similar to AArch64.
> +RISC-V's natural 4-byte instruction alignment eliminates the need for
> +additional padding NOPs.  When used with @option{-fpatchable-function-entry},
> +the type identifier is placed before any patchable NOPs. 
Note that many designs implement the "C" extension and as a result only 
have a 2 byte alignment for instructions.


Jeff

  reply	other threads:[~2025-09-16  3:40 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-05  0:24 [PATCH v2 0/7] Introduce Kernel Control Flow Integrity ABI [PR107048] Kees Cook
2025-09-05  0:24 ` [PATCH v2 1/7] mangle: Introduce C typeinfo mangling API Kees Cook
2025-09-05  0:50   ` Andrew Pinski
2025-09-05  1:09     ` Kees Cook
2025-09-05  0:24 ` [PATCH v2 2/7] kcfi: Add core Kernel Control Flow Integrity infrastructure Kees Cook
2025-09-05  8:51   ` Peter Zijlstra
2025-09-05 16:19     ` Kees Cook
2025-09-08 15:32       ` Peter Zijlstra
2025-09-08 21:55         ` Kees Cook
2025-09-09 18:49   ` Qing Zhao
2025-09-11  3:05     ` Kees Cook
2025-09-11  7:29       ` Peter Zijlstra
2025-09-12  6:20         ` Kees Cook
2025-09-11 15:04       ` Qing Zhao
2025-09-12  7:32         ` Kees Cook
2025-09-12 14:01           ` Qing Zhao
2025-09-13  6:29             ` Kees Cook
2025-09-05  0:24 ` [PATCH v2 3/7] x86: Add x86_64 Kernel Control Flow Integrity implementation Kees Cook
2025-09-05  0:24 ` [PATCH v2 4/7] aarch64: Add AArch64 " Kees Cook
2025-09-05  0:24 ` [PATCH v2 5/7] arm: Add ARM 32-bit " Kees Cook
2025-09-11  7:49   ` Ard Biesheuvel
2025-09-12  9:03     ` Kees Cook
2025-09-12  9:08       ` Kees Cook
2025-09-12  9:43         ` Ard Biesheuvel
2025-09-12 19:01           ` Kees Cook
2025-09-05  0:24 ` [PATCH v2 6/7] riscv: Add RISC-V " Kees Cook
2025-09-16  3:40   ` Jeff Law [this message]
2025-09-16  6:04     ` Kees Cook
2025-10-01  0:56       ` Jeff Law
2025-09-05  0:24 ` [PATCH v2 7/7] kcfi: Add regression test suite Kees Cook
2025-09-05  7:06   ` Jakub Jelinek
2025-09-05 17:15     ` 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=dbf9a593-e19d-4f53-96d0-d067868f40b5@gmail.com \
    --to=jeffreyalaw@gmail.com \
    --cc=andrew@sifive.com \
    --cc=ashimida.1990@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hubicka@ucw.cz \
    --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 \
    /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;
as well as URLs for NNTP newsgroup(s).