From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io0-f197.google.com (mail-io0-f197.google.com [209.85.223.197]) by kanga.kvack.org (Postfix) with ESMTP id 3A95E6B0024 for ; Tue, 27 Mar 2018 08:20:52 -0400 (EDT) Received: by mail-io0-f197.google.com with SMTP id w197so5098285iod.23 for ; Tue, 27 Mar 2018 05:20:52 -0700 (PDT) Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id j62-v6sor611240ith.99.2018.03.27.05.20.51 for (Google Transport Security); Tue, 27 Mar 2018 05:20:51 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <20180324082947.3isostkpsjraefqt@gmail.com> References: <6eb08c160ae23eb890bd937ddf8346ba211df09f.1521828274.git.andreyknvl@google.com> <20180324082947.3isostkpsjraefqt@gmail.com> From: Andrey Konovalov Date: Tue, 27 Mar 2018 14:20:49 +0200 Message-ID: Subject: Re: [RFC PATCH v2 11/15] khwasan, mm: perform untagged pointers comparison in krealloc Content-Type: text/plain; charset="UTF-8" Sender: owner-linux-mm@kvack.org List-ID: To: Ingo Molnar Cc: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Jonathan Corbet , Catalin Marinas , Will Deacon , Christoffer Dall , Marc Zyngier , Christopher Li , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Masahiro Yamada , Michal Marek , Mark Rutland , Ard Biesheuvel , Yury Norov , Nick Desaulniers , Suzuki K Poulose , Kristina Martsenko , Punit Agrawal , Dave Martin , Michael Weiser , James Morse , Julien Thierry , Steve Capper , Tyler Baicar , "Eric W . Biederman" , Thomas Gleixner , Paul Lawrence , Greg Kroah-Hartman , David Woodhouse , Sandipan Das , Kees Cook , Herbert Xu , Geert Uytterhoeven , Josh Poimboeuf , Arnd Bergmann , kasan-dev , linux-doc@vger.kernel.org, LKML , Linux ARM , kvmarm@lists.cs.columbia.edu, linux-sparse@vger.kernel.org, Linux Memory Management List , Linux Kbuild mailing list , Kostya Serebryany , Evgeniy Stepanov , Lee Smith , Ramana Radhakrishnan , Jacob Bramley , Ruben Ayrapetyan , Kees Cook , Jann Horn , Mark Brand On Sat, Mar 24, 2018 at 9:29 AM, Ingo Molnar wrote: > > * Andrey Konovalov wrote: > >> The krealloc function checks where the same buffer was reused or a new one >> allocated by comparing kernel pointers. KHWASAN changes memory tag on the >> krealloc'ed chunk of memory and therefore also changes the pointer tag of >> the returned pointer. Therefore we need to perform comparison on untagged >> (with tags reset) pointers to check whether it's the same memory region or >> not. >> >> Signed-off-by: Andrey Konovalov >> --- >> mm/slab_common.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/mm/slab_common.c b/mm/slab_common.c >> index a33e61315ca6..5911f2194cf7 100644 >> --- a/mm/slab_common.c >> +++ b/mm/slab_common.c >> @@ -1494,7 +1494,7 @@ void *krealloc(const void *p, size_t new_size, gfp_t flags) >> } >> >> ret = __do_krealloc(p, new_size, flags); >> - if (ret && p != ret) >> + if (ret && khwasan_reset_tag(p) != khwasan_reset_tag(ret)) >> kfree(p); > > Small nit: > > If 'reset' here means an all zeroes tag (upper byte) then khwasan_clear_tag() > might be a slightly easier to read primitive? 'Reset' means to set the upper byte to the value that is native for kernel pointers, and that is 0xFF. So it sets the tag to all ones, not all zeroes. I can still rename it to khwasan_clear_tag(), if you think that makes sense in this case as well.