linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] lkdtm: fix memory copy size for WRITE_KERN
@ 2021-01-27 11:05 Candle Sun
  2021-01-27 17:40 ` Nick Desaulniers
  0 siblings, 1 reply; 3+ messages in thread
From: Candle Sun @ 2021-01-27 11:05 UTC (permalink / raw)
  To: keescook
  Cc: arnd, gregkh, natechancellor, ndesaulniers, candle.sun,
	David.Laight, linux-kernel, clang-built-linux

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.

Get absolute value of the address substraction for memcpy() size.

Signed-off-by: Candle Sun <candle.sun@unisoc.com>
---
Changes in v2:
- Use abs() in place of address comparison.
---
 drivers/misc/lkdtm/perms.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/misc/lkdtm/perms.c b/drivers/misc/lkdtm/perms.c
index 2dede2ef658f..fbb7f4554054 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;
@@ -113,7 +113,7 @@ void lkdtm_WRITE_KERN(void)
 	size_t size;
 	volatile unsigned char *ptr;
 
-	size = (unsigned long)do_overwritten - (unsigned long)do_nothing;
+	size = (size_t)abs((uintptr_t)do_overwritten - (uintptr_t)do_nothing);
 	ptr = (unsigned char *)do_overwritten;
 
 	pr_info("attempting bad %zu byte write at %px\n", size, ptr);
-- 
2.17.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] lkdtm: fix memory copy size for WRITE_KERN
  2021-01-27 11:05 [PATCH v2] lkdtm: fix memory copy size for WRITE_KERN Candle Sun
@ 2021-01-27 17:40 ` Nick Desaulniers
  2021-01-28  6:46   ` Candle Sun
  0 siblings, 1 reply; 3+ messages in thread
From: Nick Desaulniers @ 2021-01-27 17:40 UTC (permalink / raw)
  To: Candle Sun
  Cc: Kees Cook, Arnd Bergmann, Greg KH, Nathan Chancellor, candle.sun,
	David Laight, LKML, clang-built-linux

On Wed, Jan 27, 2021 at 3:05 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

^ "by the compiler."

> 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*

Hopefully nothing else gets placed in between the two, otherwise we're
going to overwrite that, too.

> big and WRITE_KERN test on ARM32 arch will fail.
>
> Get absolute value of the address substraction for memcpy() size.

^ typo: subtraction

>
> Signed-off-by: Candle Sun <candle.sun@unisoc.com>
> ---
> Changes in v2:
> - Use abs() in place of address comparison.
> ---
>  drivers/misc/lkdtm/perms.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/misc/lkdtm/perms.c b/drivers/misc/lkdtm/perms.c
> index 2dede2ef658f..fbb7f4554054 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. */

^ This comment is now obsolete and should be removed together with
this patch.  That will also fix the typo in it.

> -static void do_overwritten(void)
> +static noinline void do_overwritten(void)
>  {
>         pr_info("do_overwritten wasn't overwritten!\n");
>         return;
> @@ -113,7 +113,7 @@ void lkdtm_WRITE_KERN(void)
>         size_t size;
>         volatile unsigned char *ptr;
>
> -       size = (unsigned long)do_overwritten - (unsigned long)do_nothing;
> +       size = (size_t)abs((uintptr_t)do_overwritten - (uintptr_t)do_nothing);
>         ptr = (unsigned char *)do_overwritten;
>
>         pr_info("attempting bad %zu byte write at %px\n", size, ptr);
> --
> 2.17.0
>


-- 
Thanks,
~Nick Desaulniers

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] lkdtm: fix memory copy size for WRITE_KERN
  2021-01-27 17:40 ` Nick Desaulniers
@ 2021-01-28  6:46   ` Candle Sun
  0 siblings, 0 replies; 3+ messages in thread
From: Candle Sun @ 2021-01-28  6:46 UTC (permalink / raw)
  To: Nick Desaulniers
  Cc: Kees Cook, Arnd Bergmann, Greg KH, Nathan Chancellor, Candle Sun,
	David Laight, LKML, clang-built-linux

On Thu, Jan 28, 2021 at 1:40 AM Nick Desaulniers
<ndesaulniers@google.com> wrote:
>
> On Wed, Jan 27, 2021 at 3:05 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
>
> ^ "by the compiler."
>
> > 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*
>
> Hopefully nothing else gets placed in between the two, otherwise we're
> going to overwrite that, too.
>
> > big and WRITE_KERN test on ARM32 arch will fail.
> >
> > Get absolute value of the address substraction for memcpy() size.
>
> ^ typo: subtraction
>
> >
> > Signed-off-by: Candle Sun <candle.sun@unisoc.com>
> > ---
> > Changes in v2:
> > - Use abs() in place of address comparison.
> > ---
> >  drivers/misc/lkdtm/perms.c | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/misc/lkdtm/perms.c b/drivers/misc/lkdtm/perms.c
> > index 2dede2ef658f..fbb7f4554054 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. */
>
> ^ This comment is now obsolete and should be removed together with
> this patch.  That will also fix the typo in it.
>
> > -static void do_overwritten(void)
> > +static noinline void do_overwritten(void)
> >  {
> >         pr_info("do_overwritten wasn't overwritten!\n");
> >         return;
> > @@ -113,7 +113,7 @@ void lkdtm_WRITE_KERN(void)
> >         size_t size;
> >         volatile unsigned char *ptr;
> >
> > -       size = (unsigned long)do_overwritten - (unsigned long)do_nothing;
> > +       size = (size_t)abs((uintptr_t)do_overwritten - (uintptr_t)do_nothing);
> >         ptr = (unsigned char *)do_overwritten;
> >
> >         pr_info("attempting bad %zu byte write at %px\n", size, ptr);
> > --
> > 2.17.0
> >
>
>
> --
> Thanks,
> ~Nick Desaulniers

Thanks Nick. I will clean the typo and the comment line in the next patch.

Regards,
Candle

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-01-28  6:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-27 11:05 [PATCH v2] lkdtm: fix memory copy size for WRITE_KERN Candle Sun
2021-01-27 17:40 ` Nick Desaulniers
2021-01-28  6:46   ` Candle Sun

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).