All of lore.kernel.org
 help / color / mirror / Atom feed
From: Candle Sun <candlesea@gmail.com>
To: Nick Desaulniers <ndesaulniers@google.com>
Cc: Kees Cook <keescook@chromium.org>, Arnd Bergmann <arnd@arndb.de>,
	Greg KH <gregkh@linuxfoundation.org>,
	Nathan Chancellor <natechancellor@gmail.com>,
	Candle Sun <candle.sun@unisoc.com>,
	LKML <linux-kernel@vger.kernel.org>,
	clang-built-linux <clang-built-linux@googlegroups.com>
Subject: Re: [PATCH] lkdtm: fix memory copy size for WRITE_KERN
Date: Tue, 26 Jan 2021 22:17:25 +0800	[thread overview]
Message-ID: <CAPnx3XOMpHV96aF=A3978LAapV1dXU3YvFrqU=F-Oet8xpQYpA@mail.gmail.com> (raw)
In-Reply-To: <CAKwvOd=b+ku7cd24KTYpNecYAUHxR5aBmrC1q+BL67+1YBHjzA@mail.gmail.com>

On Tue, Jan 26, 2021 at 5:16 AM Nick Desaulniers
<ndesaulniers@google.com> wrote:
>
> On Mon, Jan 25, 2021 at 12:56 AM Candle Sun <candlesea@gmail.com> wrote:
> >
> > From: Candle Sun <candle.sun@unisoc.com>
> >
> > Though do_overwritten() follows do_nothing() in source code, the final
> > memory address order is determined by compiler. We can't always assume
> > address of do_overwritten() is bigger than do_nothing(). At least the
> > Clang we are using places do_overwritten() before do_nothing() in the
> > object. This causes the copy size in lkdtm_WRITE_KERN() is *really*
> > big and WRITE_KERN test on ARM32 arch will fail.
> >
> > Compare the address order before doing the subtraction.
> >
> > Signed-off-by: Candle Sun <candle.sun@unisoc.com>
> > ---
> >  drivers/misc/lkdtm/perms.c | 19 +++++++++----------
> >  1 file changed, 9 insertions(+), 10 deletions(-)
> >
> > diff --git a/drivers/misc/lkdtm/perms.c b/drivers/misc/lkdtm/perms.c
> > index 2dede2ef658f..fbfbdf89d668 100644
> > --- a/drivers/misc/lkdtm/perms.c
> > +++ b/drivers/misc/lkdtm/perms.c
> > @@ -31,13 +31,13 @@ static unsigned long ro_after_init __ro_after_init = 0x55AA5500;
> >   * This just returns to the caller. It is designed to be copied into
> >   * non-executable memory regions.
> >   */
> > -static void do_nothing(void)
> > +static noinline void do_nothing(void)
> >  {
> >         return;
> >  }
> >
> >  /* Must immediately follow do_nothing for size calculuations to work out. */
> > -static void do_overwritten(void)
> > +static noinline void do_overwritten(void)
> >  {
> >         pr_info("do_overwritten wasn't overwritten!\n");
> >         return;
> > @@ -110,15 +110,14 @@ void lkdtm_WRITE_RO_AFTER_INIT(void)
> >
> >  void lkdtm_WRITE_KERN(void)
> >  {
> > -       size_t size;
> > -       volatile unsigned char *ptr;
> > +       unsigned long value_dow = (unsigned long)do_overwritten;
> > +       unsigned long value_do =  (unsigned long)do_nothing;
> > +       size_t size = (size_t)(value_dow > value_do ?
> > +                       value_dow - value_do : value_do - value_dow);
> >
> > -       size = (unsigned long)do_overwritten - (unsigned long)do_nothing;
>
> Thanks for the patch! Might it be simpler to do:
>
> size = abs((uintptr_t)do_overwritten - (uintptr_t)do_nothing));
>
> Then I don't think much of this function needs to be changed.
>

Thanks Nick.

abs() is better, I will check the patch.

Regards,
Candle


> > -       ptr = (unsigned char *)do_overwritten;
> > -
> > -       pr_info("attempting bad %zu byte write at %px\n", size, ptr);
> > -       memcpy((void *)ptr, (unsigned char *)do_nothing, size);
> > -       flush_icache_range((unsigned long)ptr, (unsigned long)(ptr + size));
> > +       pr_info("attempting bad %zu byte write at %px\n", size, do_overwritten);
> > +       memcpy((void *)value_dow, (void *)value_do, size);
> > +       flush_icache_range(value_dow, value_dow + (unsigned long)size);
> >         pr_err("FAIL: survived bad write\n");
> >
> >         do_overwritten();
> > --
> > 2.17.0
> >
>
>
> --
> Thanks,
> ~Nick Desaulniers

      reply	other threads:[~2021-01-26 14:19 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-25  8:56 [PATCH] lkdtm: fix memory copy size for WRITE_KERN Candle Sun
2021-01-25 10:37 ` David Laight
2021-01-26 14:13   ` Candle Sun
2021-01-26 18:39     ` Nick Desaulniers
2021-01-26 22:53       ` David Laight
2021-01-27 11:00         ` Candle Sun
2021-01-27 17:42         ` Nick Desaulniers
2021-01-25 21:16 ` Nick Desaulniers
2021-01-26 14:17   ` Candle Sun [this message]

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='CAPnx3XOMpHV96aF=A3978LAapV1dXU3YvFrqU=F-Oet8xpQYpA@mail.gmail.com' \
    --to=candlesea@gmail.com \
    --cc=arnd@arndb.de \
    --cc=candle.sun@unisoc.com \
    --cc=clang-built-linux@googlegroups.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=natechancellor@gmail.com \
    --cc=ndesaulniers@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.