From: Kees Cook <keescook@chromium.org>
To: Dan Li <ashimida@linux.alibaba.com>
Cc: Arnd Bergmann <arnd@arndb.de>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH] lkdtm: Add CFI_BACKWARD to test ROP mitigations
Date: Thu, 14 Apr 2022 10:22:19 -0700 [thread overview]
Message-ID: <202204141019.CD9152A7@keescook> (raw)
In-Reply-To: <f7a5642f-bfcb-865d-7039-d0b9d62a3360@linux.alibaba.com>
On Thu, Apr 14, 2022 at 03:19:02AM -0700, Dan Li wrote:
> Hi, Kees,
> Thanks for the rewrite. I tested this patch, and it works fine for
> me except for a few minor comments below :)
>
> On 4/13/22 14:39, Kees Cook wrote:
> > +/* The ultimate ROP gadget. */
> > +static noinline __no_ret_protection
> > +void set_return_addr_unchecked(unsigned long *expected, unsigned long *addr)
> > +{
> > + /* Use of volatile is to make sure final write isn't seen as a dead store. */
> > + unsigned long * volatile *ret_addr = (unsigned long **)__builtin_frame_address(0) + 1;
> > +
> > + /* Make sure we've found the right place on the stack before writing it. */
> > + if(*ret_addr == expected)
> > + *ret_addr = (addr);
> > + else
> > + /* Check architecture, stack layout, or compiler behavior... */
> > + pr_warn("Eek: return address mismatch! %px != %px\n",
> > + *ret_addr, addr);
> > +}
> > +
> > +static noinline
> > +void set_return_addr(unsigned long *expected, unsigned long *addr)
> > +{
> > + /* Use of volatile is to make sure final write isn't seen as a dead store. */
> > + unsigned long * volatile *ret_addr = (unsigned long **)__builtin_frame_address(0) + 1;
> > +
> > + /* Make sure we've found the right place on the stack before writing it. */
> > + if(*ret_addr == expected)
> > + *ret_addr = (addr);
>
> When PAC is enabled, I get a mismatch as follows:
>
> /kselftest_install/lkdtm # ./CFI_BACKWARD.sh
> [ 182.120133] lkdtm: Performing direct entry CFI_BACKWARD
> [ 182.120665] lkdtm: Attempting unchecked stack return address redirection ...
> [ 182.122543] lkdtm: ok: redirected stack return address.
> [ 182.123521] lkdtm: Attempting checked stack return address redirection ...
> [ 182.123964] lkdtm: Eek: return address mismatch! bfff800008fa8014 != ffff800008fa8030
> [ 182.124502] lkdtm: ok: control flow unchanged.
> CFI_BACKWARD: saw 'call trace:|ok: control flow unchanged': ok
>
> We may need to ignore the pac high bits of return address according
> to TCR.T1SZ (or simply remove the high 16 bits before comparing).
Oh! Hah, yes, I totally forgot that. Thanks for testing -- getting PAC
emulation working in QEMU has eluded me. I think untagged_addr() will
work yes? i.e.:
if((untagged_addr(*ret_addr) == expected)
>
> > + else
> > + /* Check architecture, stack layout, or compiler behavior... */
> > + pr_warn("Eek: return address mismatch! %px != %px\n",
> > + *ret_addr, addr);
>
> According to the context, it might be "expected" here?
>
> pr_warn("Eek: return address mismatch! %px != %px\n",
> *ret_addr, expected);
>
> I simply ignored the upper 16 bits, and tested it separately
> in gcc/llvm 12 with SCS/PAC and all the four cases worked fine for me.
Great! Do you have the PAC "Oops" text handy so I can include it in the
commit log as an example of what should be expected?
Thanks!
--
Kees Cook
next prev parent reply other threads:[~2022-04-14 17:22 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-13 21:39 [PATCH] lkdtm: Add CFI_BACKWARD to test ROP mitigations Kees Cook
2022-04-14 10:19 ` Dan Li
2022-04-14 17:22 ` Kees Cook [this message]
2022-04-15 8:39 ` Dan Li
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=202204141019.CD9152A7@keescook \
--to=keescook@chromium.org \
--cc=arnd@arndb.de \
--cc=ashimida@linux.alibaba.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/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).