From patchwork Wed Jun 20 17:39:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10478377 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7978060383 for ; Wed, 20 Jun 2018 17:46:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 656FA28DFB for ; Wed, 20 Jun 2018 17:46:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 58F8D28E15; Wed, 20 Jun 2018 17:46:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E89F928DFB for ; Wed, 20 Jun 2018 17:46:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754647AbeFTRqf (ORCPT ); Wed, 20 Jun 2018 13:46:35 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:37020 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754625AbeFTRkV (ORCPT ); Wed, 20 Jun 2018 13:40:21 -0400 Received: by mail-wm0-f65.google.com with SMTP id r125-v6so997271wmg.2 for ; Wed, 20 Jun 2018 10:40:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DQVDYPv0o11cjUrAzuGbiYUPrAW5THjVkKqLf3nRj8Q=; b=bC/eRfe5spyX9M4DMU+7d/hbKVAI/ovyNVLQ35g1KsIc7jF8vfd7adbFlv/lNVVYFZ pYg9K8ECl1f3U7X9Q21ZM11qgWSYygbyEf1Ao2G+FDy5WNASC1emFSyP8HJs5D878a2x Ukpjf1tkZCOY2Mi+IJZn+8FoQ4aa/P0pqDG5zMQCJoLaeRkoBTkXToa8o/wR/CThvNXz 6OcVw2WaE4mEtVL5Aj37j7yuWUJx+uriqZZdl/B+5bkQT1ndZ+OKtajBuDTs07OHU0BK 57VDygH4ZI6O3RLy+WNdHTmNE5UQyT6aBCXCJpp/hsiKVFvmn4vmLuobPfWsNNVj+qLd aNiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DQVDYPv0o11cjUrAzuGbiYUPrAW5THjVkKqLf3nRj8Q=; b=iU1ApDS73V2paVrclvQcoFcSfGB6yQZLY4Igpp7X9WFX5VdtIyXBh5cAwS4FMvR2WY VYQToiVa5/FKo6KQTLC6Qzjvcg2H1KM4lyi+WcMZqaqOT3moVm+U1dGstGOFYpqE0lxG 49V/jsl3rTC+W3JkRW1qQ4EF7/A5z2jYHDo5lcwDAvY5xh56hBeU7qXFjXYdm76pvMZZ AulBoO3jUhGaxmoDw+tOZh0KyvoB48TuX6Liycen9hotflRq/0vvOO58wi3e5cFqjyJn XMSz7bgRI4bNT6K0qUKERPQVpc4OBx1NLVvfRilqhgV9XIsa4Wy9JtaO6ehJPSbxPf/G 5p5Q== X-Gm-Message-State: APt69E0KMOHE77pqTXlvwV3lCMaraygM9XAQ37AJOjrjH119UnGdjB5u MOgmltDdSKfnYYWrte8N63ydjQ== X-Google-Smtp-Source: ADUXVKLdPuEnnanpALSfas3vcy/tsY4GPS6/W2Oo4pMabcvkmoU8mScmoLPDJZK6MasSjezAhKoWJg== X-Received: by 2002:a1c:ae8b:: with SMTP id x133-v6mr2624264wme.125.1529516419874; Wed, 20 Jun 2018 10:40:19 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id f18-v6sm4388598wro.1.2018.06.20.10.40.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Jun 2018 10:40:19 -0700 (PDT) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Catalin Marinas , Will Deacon , Christoph Lameter , Andrew Morton , Mark Rutland , Nick Desaulniers , Marc Zyngier , Dave Martin , Ard Biesheuvel , "Eric W . Biederman" , Ingo Molnar , Paul Lawrence , Geert Uytterhoeven , Arnd Bergmann , "Kirill A . Shutemov" , Greg Kroah-Hartman , Kate Stewart , Mike Rapoport , kasan-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sparse@vger.kernel.org, linux-mm@kvack.org, linux-kbuild@vger.kernel.org Cc: Kostya Serebryany , Evgeniy Stepanov , Lee Smith , Ramana Radhakrishnan , Jacob Bramley , Ruben Ayrapetyan , Jann Horn , Mark Brand , Chintan Pandya , Andrey Konovalov Subject: [PATCH v3 08/17] khwasan: add tag related helper functions Date: Wed, 20 Jun 2018 19:39:54 +0200 Message-Id: <2fc356ecb74c2a0098dacf4b46f4c131ff1513af.1529515183.git.andreyknvl@google.com> X-Mailer: git-send-email 2.18.0.rc1.244.gcf134e6275-goog In-Reply-To: References: MIME-Version: 1.0 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This commit adds a few helper functions, that are meant to be used to work with tags embedded in the top byte of kernel pointers: to set, to get or to reset (set to 0xff) the top byte. Signed-off-by: Andrey Konovalov --- arch/arm64/mm/kasan_init.c | 2 ++ include/linux/kasan.h | 23 +++++++++++++++++++ mm/kasan/khwasan.c | 47 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c index 7a31e8ccbad2..e7f37c0b7e14 100644 --- a/arch/arm64/mm/kasan_init.c +++ b/arch/arm64/mm/kasan_init.c @@ -250,6 +250,8 @@ void __init kasan_init(void) memset(kasan_zero_page, KASAN_SHADOW_INIT, PAGE_SIZE); cpu_replace_ttbr1(lm_alias(swapper_pg_dir)); + khwasan_init(); + /* At this point kasan is fully initialized. Enable error messages */ init_task.kasan_depth = 0; pr_info("KernelAddressSanitizer initialized\n"); diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 336385baf926..d7624b879d86 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -155,6 +155,29 @@ static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} #define KASAN_SHADOW_INIT 0xFF +void khwasan_init(void); + +void *khwasan_set_tag(const void *addr, u8 tag); +u8 khwasan_get_tag(const void *addr); +void *khwasan_reset_tag(const void *ptr); + +#else /* CONFIG_KASAN_HW */ + +static inline void khwasan_init(void) { } + +static inline void *khwasan_set_tag(const void *addr, u8 tag) +{ + return (void *)addr; +} +static inline u8 khwasan_get_tag(const void *addr) +{ + return 0xFF; +} +static inline void *khwasan_reset_tag(const void *ptr) +{ + return (void *)ptr; +} + #endif /* CONFIG_KASAN_HW */ #endif /* LINUX_KASAN_H */ diff --git a/mm/kasan/khwasan.c b/mm/kasan/khwasan.c index e2c3a7f7fd1f..d34679b8f8c7 100644 --- a/mm/kasan/khwasan.c +++ b/mm/kasan/khwasan.c @@ -38,6 +38,53 @@ #include "kasan.h" #include "../slab.h" +static DEFINE_PER_CPU(u32, prng_state); + +void khwasan_init(void) +{ + int cpu; + + for_each_possible_cpu(cpu) + per_cpu(prng_state, cpu) = get_random_u32(); +} + +/* + * If a preemption happens between this_cpu_read and this_cpu_write, the only + * side effect is that we'll give a few allocated in different contexts objects + * the same tag. Since KHWASAN is meant to be used a probabilistic bug-detection + * debug feature, this doesn’t have significant negative impact. + * + * Ideally the tags use strong randomness to prevent any attempts to predict + * them during explicit exploit attempts. But strong randomness is expensive, + * and we did an intentional trade-off to use a PRNG. This non-atomic RMW + * sequence has in fact positive effect, since interrupts that randomly skew + * PRNG at unpredictable points do only good. + */ +u8 random_tag(void) +{ + u32 state = this_cpu_read(prng_state); + + state = 1664525 * state + 1013904223; + this_cpu_write(prng_state, state); + + return (u8)(state % (KHWASAN_TAG_MAX + 1)); +} + +void *khwasan_set_tag(const void *addr, u8 tag) +{ + return set_tag(addr, tag); +} + +u8 khwasan_get_tag(const void *addr) +{ + return get_tag(addr); +} + +void *khwasan_reset_tag(const void *addr) +{ + return reset_tag(addr); +} + void check_memory_region(unsigned long addr, size_t size, bool write, unsigned long ret_ip) {