From: Mark Rutland <mark.rutland@arm.com>
To: Fangrui Song <maskray@google.com>
Cc: Sami Tolvanen <samitolvanen@google.com>,
llvm@lists.linux.dev, linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
Joao Moreira <joao@overdrivepizza.com>,
Josh Poimboeuf <jpoimboe@redhat.com>,
Kees Cook <keescook@chromium.org>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Peter Zijlstra <peterz@infradead.org>
Subject: Re: kCFI && patchable-function-entry=M,N
Date: Mon, 24 Oct 2022 12:18:26 +0100 [thread overview]
Message-ID: <Y1Z0gvscALn2br2q@FVFF77S0Q05N> (raw)
In-Reply-To: <CAFP8O3JuC9HL1-G23vGJO=jjN7Tsgz7c8TB1_F6UQ-MqCyGNLQ@mail.gmail.com>
On Fri, Oct 21, 2022 at 09:14:41PM -0700, Fangrui Song wrote:
> On Fri, Oct 21, 2022 at 10:39 AM Sami Tolvanen <samitolvanen@google.com> wrote:
> >
> > On Fri, Oct 21, 2022 at 8:56 AM Mark Rutland <mark.rutland@arm.com> wrote:
> > >
> > > Hi,
> > >
> > > For arm64, I'd like to use -fatchable-function-entry=M,N (where N > 0), for our
> > > ftrace implementation, which instruments *some* but not all functions.
> > > Unfortuntately, this doesn't play nicely with -fsanitize=kcfi, as instrumented
> > > and non-instrumented functions don't agree on where the type hash should live
> > > relative to the function entry point, making them incompatible with one another.
> >
> > Yes, the current implementation assumes that if prefix nops are used,
> > all functions have the same number of them.
> >
> > > Is there any mechanism today that we could use to solve this, or could we
> > > extend clang to have some options to control this behaviour?
> >
> > I don't think there's a mechanism to work around the issue right now,
> > but we could just change where the hash is emitted on arm64.
> >
> > > It would also be helpful to have a symbol before both the hash and pre-function
> > > NOPs so that we can filter those out of probes patching (I see that x86 does
> > > this with the __cfi_function symbol).
> >
> > Adding a symbol before the hash isn't a problem, but if we move the
> > hash and want the symbol to be placed before the prefix nops as well,
> > we might need a flag to control this. Fangrui, what do you think?
> >
> > Sami
>
> Since the kcfi code expects the hash to appear in a specific location
> so that an instrumented indirect jump can reliably obtain the hash.
> For a translation unit `-fpatchable-function-entry=N,M` may be
> specified or not, and we want both to work. Therefore, I agree that a
> consistent hash location will help. This argument favors placing M
> nops before the hash. The downside is a restriction on how the M nops
> can be used. Previously if M>0, the runtime code needs to check
> whether a BTI exists to locate the N-M after-function-entry NOPs. If
> the hash appears after the M nops, the runtime code needs to
> additionally knows whether the hash exists. My question is how to
> reliably detect this.
That's a fair point.
For detecting BTI we can scan the binary for BTI/NOP at M instructions into the
patch-site, but a similar approach won't be reliable for the type hash since
the type hash itself could have the same bit pattern as an instruction.
> If there is motivation using M>0, I'd like to know the concrete code
> sequence for `-fpatchable-function-entry=N,M` and how the runtime code
> detects the NOPs with optional hash and optional BTI.
For the BTI case alone, I have code at:
https://git.kernel.org/pub/scm/linux/kernel/git/mark/linux.git/commit/?h=arm64/ftrace/per-callsite-ops&id=272a580fd5b7acc31747505d71530cee7cc2837d
... the gist being that it checks the instruction M insns after the start of
the patch site.
For the type hash, I think there are a few options, e.g.
* Restricting the type hash to a set of values that can be identified (e.g.
encoding those as a permanently-undefined UDF with a 16-bit immediate).
* Adding options to record additional metadata along with the pointer to the
patch-site in the __patchable_function_entries section.
* Adding an option to record patch-site variants to sub-sections of the
__patchable_function_entries section, so that at link time these can be
grouped separately, e.g.
* __patchable_function_entries.??? // no BTI, no type hash
* __patchable_function_entries.bti // has BTI
* __patchable_function_entries.bti_cfi // has BTI and type hash
* __patchable_function_entries.cfi // has type hash
Do any of those approaches sound plausible to you?
Thanks,
Mark.
WARNING: multiple messages have this Message-ID (diff)
From: Mark Rutland <mark.rutland@arm.com>
To: Fangrui Song <maskray@google.com>
Cc: Sami Tolvanen <samitolvanen@google.com>,
llvm@lists.linux.dev, linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
Joao Moreira <joao@overdrivepizza.com>,
Josh Poimboeuf <jpoimboe@redhat.com>,
Kees Cook <keescook@chromium.org>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Peter Zijlstra <peterz@infradead.org>
Subject: Re: kCFI && patchable-function-entry=M,N
Date: Mon, 24 Oct 2022 12:18:26 +0100 [thread overview]
Message-ID: <Y1Z0gvscALn2br2q@FVFF77S0Q05N> (raw)
In-Reply-To: <CAFP8O3JuC9HL1-G23vGJO=jjN7Tsgz7c8TB1_F6UQ-MqCyGNLQ@mail.gmail.com>
On Fri, Oct 21, 2022 at 09:14:41PM -0700, Fangrui Song wrote:
> On Fri, Oct 21, 2022 at 10:39 AM Sami Tolvanen <samitolvanen@google.com> wrote:
> >
> > On Fri, Oct 21, 2022 at 8:56 AM Mark Rutland <mark.rutland@arm.com> wrote:
> > >
> > > Hi,
> > >
> > > For arm64, I'd like to use -fatchable-function-entry=M,N (where N > 0), for our
> > > ftrace implementation, which instruments *some* but not all functions.
> > > Unfortuntately, this doesn't play nicely with -fsanitize=kcfi, as instrumented
> > > and non-instrumented functions don't agree on where the type hash should live
> > > relative to the function entry point, making them incompatible with one another.
> >
> > Yes, the current implementation assumes that if prefix nops are used,
> > all functions have the same number of them.
> >
> > > Is there any mechanism today that we could use to solve this, or could we
> > > extend clang to have some options to control this behaviour?
> >
> > I don't think there's a mechanism to work around the issue right now,
> > but we could just change where the hash is emitted on arm64.
> >
> > > It would also be helpful to have a symbol before both the hash and pre-function
> > > NOPs so that we can filter those out of probes patching (I see that x86 does
> > > this with the __cfi_function symbol).
> >
> > Adding a symbol before the hash isn't a problem, but if we move the
> > hash and want the symbol to be placed before the prefix nops as well,
> > we might need a flag to control this. Fangrui, what do you think?
> >
> > Sami
>
> Since the kcfi code expects the hash to appear in a specific location
> so that an instrumented indirect jump can reliably obtain the hash.
> For a translation unit `-fpatchable-function-entry=N,M` may be
> specified or not, and we want both to work. Therefore, I agree that a
> consistent hash location will help. This argument favors placing M
> nops before the hash. The downside is a restriction on how the M nops
> can be used. Previously if M>0, the runtime code needs to check
> whether a BTI exists to locate the N-M after-function-entry NOPs. If
> the hash appears after the M nops, the runtime code needs to
> additionally knows whether the hash exists. My question is how to
> reliably detect this.
That's a fair point.
For detecting BTI we can scan the binary for BTI/NOP at M instructions into the
patch-site, but a similar approach won't be reliable for the type hash since
the type hash itself could have the same bit pattern as an instruction.
> If there is motivation using M>0, I'd like to know the concrete code
> sequence for `-fpatchable-function-entry=N,M` and how the runtime code
> detects the NOPs with optional hash and optional BTI.
For the BTI case alone, I have code at:
https://git.kernel.org/pub/scm/linux/kernel/git/mark/linux.git/commit/?h=arm64/ftrace/per-callsite-ops&id=272a580fd5b7acc31747505d71530cee7cc2837d
... the gist being that it checks the instruction M insns after the start of
the patch site.
For the type hash, I think there are a few options, e.g.
* Restricting the type hash to a set of values that can be identified (e.g.
encoding those as a permanently-undefined UDF with a 16-bit immediate).
* Adding options to record additional metadata along with the pointer to the
patch-site in the __patchable_function_entries section.
* Adding an option to record patch-site variants to sub-sections of the
__patchable_function_entries section, so that at link time these can be
grouped separately, e.g.
* __patchable_function_entries.??? // no BTI, no type hash
* __patchable_function_entries.bti // has BTI
* __patchable_function_entries.bti_cfi // has BTI and type hash
* __patchable_function_entries.cfi // has type hash
Do any of those approaches sound plausible to you?
Thanks,
Mark.
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2022-10-24 11:18 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-21 15:56 kCFI && patchable-function-entry=M,N Mark Rutland
2022-10-21 15:56 ` Mark Rutland
2022-10-21 17:39 ` Sami Tolvanen
2022-10-21 17:39 ` Sami Tolvanen
2022-10-22 4:14 ` Fangrui Song
2022-10-22 4:14 ` Fangrui Song
2022-10-24 11:18 ` Mark Rutland [this message]
2022-10-24 11:18 ` Mark Rutland
2022-10-24 18:37 ` Sami Tolvanen
2022-10-24 18:37 ` Sami Tolvanen
2022-10-22 14:57 ` Peter Zijlstra
2022-10-22 14:57 ` Peter Zijlstra
2022-10-24 11:24 ` Mark Rutland
2022-10-24 11:24 ` Mark Rutland
2023-01-04 17:30 ` Fangrui Song
2023-01-04 17:30 ` Fangrui Song
2025-04-03 20:15 ` Sami Tolvanen
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=Y1Z0gvscALn2br2q@FVFF77S0Q05N \
--to=mark.rutland@arm.com \
--cc=joao@overdrivepizza.com \
--cc=jpoimboe@redhat.com \
--cc=keescook@chromium.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=maskray@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=peterz@infradead.org \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.