From patchwork Tue May 8 17:20: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: 10386623 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 828F4602C2 for ; Tue, 8 May 2018 17:21:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6F9C2290B3 for ; Tue, 8 May 2018 17:21:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 61E75290B9; Tue, 8 May 2018 17:21:44 +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=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AFE61290B3 for ; Tue, 8 May 2018 17:21:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F1FE06B02C9; Tue, 8 May 2018 13:21:29 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id ED1056B02CA; Tue, 8 May 2018 13:21:29 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C61F16B02CB; Tue, 8 May 2018 13:21:29 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wr0-f198.google.com (mail-wr0-f198.google.com [209.85.128.198]) by kanga.kvack.org (Postfix) with ESMTP id 635586B02C9 for ; Tue, 8 May 2018 13:21:29 -0400 (EDT) Received: by mail-wr0-f198.google.com with SMTP id g7-v6so22229791wrb.19 for ; Tue, 08 May 2018 10:21:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version:in-reply-to :references:content-transfer-encoding; bh=oqHzE+FzroTuiY9Rlyx6BhGu7zN9wbh/wKV4Dbdj1Z8=; b=mw2nDrNR9JlF403PnFn71oaUpPw3nQ20GSAjpuP1RaOn/zPoQ5bHh2LQPMEH8d52tX z2Hu+0nMtet9fE5nSROk2M/QW+cm7YEWKKDCYG49eKQz8Is4VGvYTrDqe3bAHl6U85Qc BFK6FGTG3EENWuYSKDOsKoLC1mj4o1kchBybpMiFH5JY873OeB6/CD5fmB01a1VA9sPI GwPF/f+m7HKo+bg9KhO4y/ue+DNwJi+0MWPZO8B9pdfJvE4ezcYsUKfHPWWASs9yX5eh zRcIpG7oJFd0G9lEByXyuilxH4PZQmwMiepURYW5O82wHXdlbTorE92SZ6XH6O3kgfNP MSrg== X-Gm-Message-State: ALKqPwdTb+iwetg+vVeEniwEkcLmmK7A/2FgL3wA96J4+BuFjWeCLr6A PAkB435BucPwFA9za/tAJAIg1jFc36N8s1fntobdVh7ePRCemMBcV8rFzAG6yrduweLx/+w3nFF IW+E+vOa8YnnNz5jj7SAPZps/UiznKD/XUvTJOtUrhq0JFzQk54U3g+gscJ20QEYwcqJuaTJLVu dhAUBAorMp8ZG/IqrBWBgROGTEbq1A5A+9Bo+HFLdPpwATbHTKST0isTVO+Myh2/Il4HEBegI8d XU3sugWd9UGxATkNpYdVwrh6RINZ+DoX7LJjLLF3OQh2mdLRPkwQ7YR5ePv/ebnG15gmc8nJYPU wRJKhDYPXvwyxHCiA5vs5bO2C+nvYWHMcRwDLVIhmt8dIBxKkQ0DUV1lN5S6mJZCibniQir/UUS 6 X-Received: by 2002:adf:a219:: with SMTP id p25-v6mr3036065wra.19.1525800088943; Tue, 08 May 2018 10:21:28 -0700 (PDT) X-Received: by 2002:adf:a219:: with SMTP id p25-v6mr3035995wra.19.1525800088096; Tue, 08 May 2018 10:21:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525800088; cv=none; d=google.com; s=arc-20160816; b=Cs7aMeKiaFoh7wMougLTKszdC32YVqPaSH7ZwALbCYxoMsyKlYhkW7dDQDgV/jQQPc ZWDeP+JrECX3cUQey56ocNe4KxB/R6u50LjNNU1rOHfdcMTT4kb0LlbeLJXw057ITPoR aAIhlEISNIJtju6jRZRRVL7xBUZxa/R1RlaAYoWKpblUC01CXwP1ceRs8AkiasTS1lWQ V1yYeSNUSuoZP9RaZwl6HhiQOV4nKfYyUbMZ83t6+gNTFSjbQHd/ZdynZfule0T/oFtz ljWqivtzTgPJhgTKbrDFMOIRsPvqxcCK9QPZUgpFXnx1HkHePs/TJAagqcFV51GUfR6G Ue0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:references:in-reply-to:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=oqHzE+FzroTuiY9Rlyx6BhGu7zN9wbh/wKV4Dbdj1Z8=; b=nneQNrYEsk8BugsLpxbr3XnJWV1BK+AStOBDd8feAhkJhwGRrO6j7B/OUy5FeyPpW8 +jLRPuAr+ZJlSb98g345wsBzVGwVNM5JM/hRUC5+NAIZYvWJD74R3zOXCwNZCGXbsHS5 4gExttzIWjC+vC4ZdNjIB8WvjZNOTfd8YGBlQyIEEmDBCc+RJSWQaWYInNTXIlJ2zaXr yJuwuxnxJDSI8N84MDnXTQI5RDjSFJPgX3SDRrJRxgQMEudk9a1NOoAdjd6GAOnR5nfB ZdQTM3Y+qDxCSzkBW5qrV5rb4EGYCXoMCHtT7NkoJ+BphujI4hNBuJiTjpdORcRmgfNL 65Gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=r3V3dmiJ; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id z21-v6sor842304wrb.10.2018.05.08.10.21.27 for (Google Transport Security); Tue, 08 May 2018 10:21:28 -0700 (PDT) Received-SPF: pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=r3V3dmiJ; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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:in-reply-to:references:content-transfer-encoding; bh=oqHzE+FzroTuiY9Rlyx6BhGu7zN9wbh/wKV4Dbdj1Z8=; b=r3V3dmiJu9t/QSGiXAKNfvdszGBXZpWUqFC/WQl8lJTJwq8zcBgfHPO9QvWePVuHWV Uu7oV5GFrWhDgTKHLwqsqinsh4GvOcdIBKrRLTaXsLq3yT23aqzs4x8i+7D07Ao45D0F MbC4jlAZDRNFWk9BlP2skloVaCt1XWfDJMLcV9zbmmOGlpWse2xOLqA9SeRCNV0VUkUz kKROuw2tGrg+h9987s+0rZy4HBizBYDeKbk6idj7tc77oBAoaLMtsCggFFsQPxuYDbun /njHsLQjvDMGxqHnSKQbwvmVOamcZGCyhOoBk+khZY5Sm5+FCHi/HEE/6jf129RY4NGn HFaA== X-Google-Smtp-Source: AB8JxZqoBEWtUlS/svVglkwwEES6Do5DINGYGV55mg5r7YpM4iZ0SiEe07lypGsYVTXZPIT2oWwVLw== X-Received: by 2002:adf:db0b:: with SMTP id s11-v6mr32136748wri.241.1525800087080; Tue, 08 May 2018 10:21:27 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id m134sm14178311wmg.4.2018.05.08.10.21.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 May 2018 10:21:25 -0700 (PDT) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Jonathan Corbet , Catalin Marinas , Will Deacon , Christopher Li , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Masahiro Yamada , Michal Marek , Andrey Konovalov , Mark Rutland , Nick Desaulniers , Yury Norov , Marc Zyngier , Kristina Martsenko , Suzuki K Poulose , Punit Agrawal , Dave Martin , Ard Biesheuvel , James Morse , Michael Weiser , Julien Thierry , Tyler Baicar , "Eric W . Biederman" , Thomas Gleixner , Ingo Molnar , Kees Cook , Sandipan Das , David Woodhouse , Paul Lawrence , Herbert Xu , Josh Poimboeuf , Geert Uytterhoeven , Tom Lendacky , Arnd Bergmann , Dan Williams , Michal Hocko , Jan Kara , Ross Zwisler , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Matthew Wilcox , "Kirill A . Shutemov" , Souptick Joarder , Hugh Dickins , Davidlohr Bueso , Greg Kroah-Hartman , Philippe Ombredanne , Kate Stewart , Laura Abbott , Boris Brezillon , Vlastimil Babka , Pintu Agarwal , Doug Berger , Anshuman Khandual , Mike Rapoport , Mel Gorman , Pavel Tatashin , Tetsuo Handa , 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 , Kees Cook , Jann Horn , Mark Brand , Chintan Pandya Subject: [PATCH v1 08/16] khwasan: add tag related helper functions Date: Tue, 8 May 2018 19:20:54 +0200 Message-Id: <6a2a088b08c6f5e87c32892ec28b139a99d06070.1525798754.git.andreyknvl@google.com> X-Mailer: git-send-email 2.17.0.441.gb46fe60e1d-goog In-Reply-To: References: MIME-Version: 1.0 In-Reply-To: References: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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/kasan.h | 55 ++++++++++++++++++++++++++++++++++++++ mm/kasan/khwasan.c | 48 +++++++++++++++++++++++++++++++++ 4 files changed, 128 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/kasan.h b/mm/kasan/kasan.h index 5091a433f266..cd51ae9d8149 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -8,6 +8,10 @@ #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT) #define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1) +#define KHWASAN_TAG_KERNEL 0xFF /* native kernel pointers tag */ +#define KHWASAN_TAG_INVALID 0xFE /* inaccessible memory tag */ +#define KHWASAN_TAG_MAX 0xFD /* maximum value for random tags */ + #define KASAN_FREE_PAGE 0xFF /* page was freed */ #define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */ #define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */ @@ -135,6 +139,57 @@ static inline void quarantine_reduce(void) { } static inline void quarantine_remove_cache(struct kmem_cache *cache) { } #endif +#ifdef CONFIG_KASAN_HW + +#define KHWASAN_TAG_SHIFT 56 +#define KHWASAN_TAG_MASK (0xFFUL << KHWASAN_TAG_SHIFT) + +u8 random_tag(void); + +static inline void *set_tag(const void *addr, u8 tag) +{ + u64 a = (u64)addr; + + a &= ~KHWASAN_TAG_MASK; + a |= ((u64)tag << KHWASAN_TAG_SHIFT); + + return (void *)a; +} + +static inline u8 get_tag(const void *addr) +{ + return (u8)((u64)addr >> KHWASAN_TAG_SHIFT); +} + +static inline void *reset_tag(const void *addr) +{ + return set_tag(addr, KHWASAN_TAG_KERNEL); +} + +#else /* CONFIG_KASAN_HW */ + +static inline u8 random_tag(void) +{ + return 0; +} + +static inline void *set_tag(const void *addr, u8 tag) +{ + return (void *)addr; +} + +static inline u8 get_tag(const void *addr) +{ + return 0; +} + +static inline void *reset_tag(const void *addr) +{ + return (void *)addr; +} + +#endif /* CONFIG_KASAN_HW */ + /* * Exported functions for interfaces called from assembly or from generated * code. Declarations here to avoid warning about missing declarations. diff --git a/mm/kasan/khwasan.c b/mm/kasan/khwasan.c index e2c3a7f7fd1f..4e253c1e4d35 100644 --- a/mm/kasan/khwasan.c +++ b/mm/kasan/khwasan.c @@ -38,6 +38,54 @@ #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) {