linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff Law <jeffreyalaw@gmail.com>
To: Kees Cook <kees@kernel.org>
Cc: Qing Zhao <qing.zhao@oracle.com>,
	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: Tue, 30 Sep 2025 18:56:12 -0600	[thread overview]
Message-ID: <965b7366-2474-4712-9f3c-4cd71f63d17d@gmail.com> (raw)
In-Reply-To: <202509152254.8A38F96D91@keescook>



On 9/16/25 12:04 AM, Kees Cook wrote:

>>> +/* 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?
> 
> Sure, I can document those more fully in the next version.
Thanks.


> 
>>> +riscv_output_kcfi_insn (rtx_insn *insn, rtx *operands)
>>> +{
>>> +  /* 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?
> 
> Yes, these all come from their respective RTL's:
> 
>           (match_operand 3 "const_int_operand"))
Perfect.  Just wanted to make sure.  It's a fairly common goof to try to 
extract an integer value from a non-integer node.


>> 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.
> 
> Yeah, this needs more careful management of the scratch registers. I
> have not been able to find a sane way to provide working constraints to
> the RTL patterns, but I'd _much_ rather let the register allocator do
> all this work.
Usually you end up having to define a register class with the single 
register you want.  Of course once you do that you also need to start 
defining union classes and you also have to audit all kinds of code to 
make sure it's doing something sensible.  Yea, it's painful.


> 
>>> +
>>> +  /* 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?
> 
> It was designed entirely for rv64. I'm not against making it work with
> rv32, but I just haven't tried or tested it there.
ACK.  This may never end up being used on rv32.  But we should at least 
fix the obvious stuff since it's just the right thing to do.

Conceptually any pointer should be using Pmode.  If you keep that in 
mind, that covers one big blob of issues.  It also means that if someone 
where to try to light up 32 bit pointers on rv64 that your code is ready 
for that (and yes, we've had those kinds of requests, though to date 
none of that code has been ready to integrate).


>>
>> 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?
> 
> Yeah, I covered this a bit in patch #2 in the series which describe the
> design requirements. The main issue is that the typeid validation check
> cannot be separated from the call, and the instruction pattern needs to
> have very close control over the register usage so we don't introduce
> any new indirect call gadgets (pop %target, call %target).
> 
> So, this is a replacement of the regular CALL rtl pattern. I am totally
> open to any other way to do this. I have been bumbling around in here
> (and on the other architectures) trying to find ways to make it all
> work, but it still feels like a bit of a hack. :)
I didn't really see anything in patch#2 which would indicate we want to 
generate blobs of assembly code.  It feels like there's something 
missing in both our understandings.





> 
> Okay, noted. Are there any restrictions on function pointer alignment?
> Regardless, I should probably rewrite this language a bit to try to
> better say "we don't care about alignment padding since the preamble
> typeid contents are a multiple of instruction size" (which would still
> be true for 2 byte alignemnt).
Nope.  The architecture will require them to be 2 byte aligned if "C" is 
enabled or 4 byte aligned if "C" is not enabled.  In both cases those 
alignments correspond to the minimum instruction size.

Jeff

  reply	other threads:[~2025-10-01  0:56 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
2025-09-16  6:04     ` Kees Cook
2025-10-01  0:56       ` Jeff Law [this message]
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=965b7366-2474-4712-9f3c-4cd71f63d17d@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).