From: Kees Cook <kees@kernel.org>
To: Ard Biesheuvel <ardb@kernel.org>
Cc: Qing Zhao <qing.zhao@oracle.com>,
Andrew Pinski <pinskia@gmail.com>,
Jakub Jelinek <jakub@redhat.com>,
Martin Uecker <uecker@tugraz.at>,
Richard Biener <rguenther@suse.de>,
Joseph Myers <josmyers@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Jeff Law <jeffreyalaw@gmail.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>,
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 v4 6/7] arm: Add ARM 32-bit Kernel Control Flow Integrity implementation
Date: Mon, 29 Sep 2025 23:18:46 -0700 [thread overview]
Message-ID: <202509292310.BB6055DC@keescook> (raw)
In-Reply-To: <CAMj1kXHQn0YEvpnwRigL_Mu61SGtGfmKVUJJgotGmm8S_BT8dA@mail.gmail.com>
On Mon, Sep 29, 2025 at 11:59:15AM +0200, Ard Biesheuvel wrote:
> On Fri, 26 Sept 2025 at 05:02, Kees Cook <kees@kernel.org> wrote:
> >
> > diff --git a/gcc/config/arm/arm.md b/gcc/config/arm/arm.md
> > index 422ae549b65b..c3b9f16ea872 100644
> > --- a/gcc/config/arm/arm.md
> > +++ b/gcc/config/arm/arm.md
> ...
> > +/* Output the assembly for a KCFI checked call instruction. INSN is the
> > + RTL instruction being processed. OPERANDS is the array of RTL operands
> > + where operands[0] is the call target register, operands[2] is the KCFI
> > + type ID constant. Returns an empty string as all output is handled by
> > + direct assembly generation. */
> > +
> > +const char *
> > +arm_output_kcfi_insn (rtx_insn *insn, rtx *operands)
> > +{
> > + /* KCFI type id. */
> > + uint32_t type_id = INTVAL (operands[2]);
> > +
> > + /* Calculate typeid offset from call target. */
> > + HOST_WIDE_INT offset = -kcfi_typeid_offset;
> > +
> > + /* Generate custom label names. */
> > + char trap_name[32];
> > + char call_name[32];
> > + ASM_GENERATE_INTERNAL_LABEL (trap_name, "Lkcfi_trap", kcfi_labelno);
> > + ASM_GENERATE_INTERNAL_LABEL (call_name, "Lkcfi_call", kcfi_labelno);
> > +
> > + /* Create memory operand for the type load. */
> > + rtx mem_op = gen_rtx_MEM (SImode,
> > + gen_rtx_PLUS (SImode, operands[0],
> > + GEN_INT (offset)));
> > + rtx temp_operands[6];
> > +
> > + /* Normally we can use r12 as our scratch register. */
> > + unsigned scratch_reg_num = IP_REGNUM;
> > + /* If register pressure has made r12 our target register, we need to pick
> > + a different register. We don't want to spill our target register
> > + because on reload at the end of the KCFI check, we'd be producing
> > + the very kind of call gadget we were trying to protect against:
> > + "pop %target; call %target". In this case, use r3 as our scratch
> > + register. But since r3 may be used for function arguments, we need
> > + to check if it is being used for that and only spill/reload if that
> > + happens. Any spill/reload of r3 due to making a call will already
> > + have been managed by the register allocator, so we only have to care
> > + about not clobbering the argument value it may be carrying into the
> > + call here. Also use r3 when r12 is a fixed register. */
> > + if (REGNO (operands[0]) == scratch_reg_num
> > + || fixed_regs[scratch_reg_num])
> > + scratch_reg_num = LAST_ARG_REGNUM;
> > + rtx scratch_reg = gen_rtx_REG (SImode, scratch_reg_num);
> > +
> > + /* We only need to spill r3 if it's actually used by the call. */
> > + bool need_spill = (scratch_reg_num == LAST_ARG_REGNUM)
> > + && reg_overlap_mentioned_p (scratch_reg, insn);
> > +
> > + /* Calculate trap immediate. */
> > + unsigned addr_reg_num = REGNO (operands[0]);
> > + /* The scratch register is always clobbered by eor seq: use 0x1F. */
> > + unsigned udf_immediate = 0x8000 | (0x1F << 5) | (addr_reg_num & 31);
> > +
>
> I take it this means you still need to decode the instructions in the
> kernel to obtain the expected type id?
Currently, yes.
> Can't you insert the actual register index here, and defer the reload
> until after the UDF? That way, the scratch register will always
> contain the XOR of the actual vs expected typeids when taking the
> trap.
My instinct is to avoid any kind of load/call gadget (as a ROP target),
even if the controlled register is only the 4th argument. The risk is
much lower, but it seemed like reducing the risk to 0 requires just a
little help on the kernel side after taking the trap (and x86 already
does this reliably).
I suppose as an alternative I could use the index when it's not r3, but
then Linux would need to read the destination memory to rebulid the XOR?
I think that's even more fragile... I think it'd be best to just read
back the prior 5 instructions before the trap. It's reliable. :)
-Kees
--
Kees Cook
next prev parent reply other threads:[~2025-09-30 6:18 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-26 3:02 [PATCH v4 0/7] Introduce Kernel Control Flow Integrity ABI [PR107048] Kees Cook
2025-09-26 3:02 ` [PATCH v4 1/7] typeinfo: Introduce KCFI typeinfo mangling API Kees Cook
2025-09-29 10:34 ` Martin Uecker
2025-09-30 6:21 ` Kees Cook
2025-10-03 19:21 ` Qing Zhao
2025-10-14 23:32 ` Kees Cook
2025-09-26 3:02 ` [PATCH v4 2/7] kcfi: Add core Kernel Control Flow Integrity infrastructure Kees Cook
2025-10-02 14:56 ` Qing Zhao
2025-10-14 23:28 ` Kees Cook
2025-10-15 18:05 ` Qing Zhao
2025-09-26 3:02 ` [PATCH v4 3/7] kcfi: Add regression test suite Kees Cook
2025-09-26 3:02 ` [PATCH v4 4/7] x86: Add x86_64 Kernel Control Flow Integrity implementation Kees Cook
2025-09-26 3:02 ` [PATCH v4 5/7] aarch64: Add AArch64 " Kees Cook
2025-09-26 3:02 ` [PATCH v4 6/7] arm: Add ARM 32-bit " Kees Cook
2025-09-26 4:06 ` Kees Cook
2025-09-29 9:59 ` Ard Biesheuvel
2025-09-30 6:18 ` Kees Cook [this message]
2025-09-26 3:02 ` [PATCH v4 7/7] riscv: Add RISC-V " 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=202509292310.BB6055DC@keescook \
--to=kees@kernel.org \
--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=jeffreyalaw@gmail.com \
--cc=jim.wilson.gcc@gmail.com \
--cc=joao@overdrivepizza.com \
--cc=josmyers@redhat.com \
--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=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