From patchwork Fri May 25 14:40:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10427669 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 07445602D6 for ; Fri, 25 May 2018 14:41:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E8459297C0 for ; Fri, 25 May 2018 14:41:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DC1A4297BB; Fri, 25 May 2018 14:41:18 +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 A7C6E297C0 for ; Fri, 25 May 2018 14:41:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D99F16B0266; Fri, 25 May 2018 10:40:58 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D495B6B0269; Fri, 25 May 2018 10:40:58 -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 B9A8C6B026A; Fri, 25 May 2018 10:40:58 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wr0-f200.google.com (mail-wr0-f200.google.com [209.85.128.200]) by kanga.kvack.org (Postfix) with ESMTP id 57FFE6B0266 for ; Fri, 25 May 2018 10:40:58 -0400 (EDT) Received: by mail-wr0-f200.google.com with SMTP id i2-v6so3489719wrm.5 for ; Fri, 25 May 2018 07:40:58 -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 :content-transfer-encoding; bh=q3sk7ufsfcpLhhRUqQfALsmeeTCJvnJNnm+o4i2Xa54=; b=pQE962ewWQ3NaZTu4TclWfhmVGEun3EEuSJXUjYE+e1KNx0Wb5XiX4XgI9yjcGU1Tw Iib2OUVWndqKkRk/EoNh+n1jq3JsgxNJF4ZFbY8GVyBylCPAdwX6sry0N/dA/ZZ37UUa /Vavr9WOoYoUeeAD7KpzI+u40QuUQ/fZw2Iznyl4Npen3SCw/uAqLFWZFuaEXmOUtY0B qS7EmthyPE4+5pU0x1SW2JX+4iKyv7FPBe4oceU0xwrebZWumVIQXu9jK2JpY5EUxGJ5 7NvN7vqxMPwnX2eELA283JhRvtFsN08SWlVUHzgWweOFLNDLDqaZ0LV56p6UDnMUgKlC aZ5g== X-Gm-Message-State: ALKqPwce4hBB0i6nlybVLShjDx2BMhY0Es1hXGXSwPHwr+Vsq9gkHPoN 4maSRJh2KghOJLSKZVehi7Kp8eRtDQgGBSP9OROLAQeLTESA2/W7SuTGM4EcL/2tzAvumIDA6LF 0xaX1NZZtkD8QhaXDCDDMmW0juvYWNe5QESJKJyWn6QYB/9llGjCLLN0pEjEUFdZNyk9Ie05+6o 2MdnFpK76IWv+oepmVVAryB6Mt1qsV0NSYO4bzgYASiebzksujTT19hA2NefyWL6hERo6n2+3Ti AvbzOC3mVJImVVwL2yI6MzHOkm4AbnP5wcbXIMqWHG0tfSSSZiBytGESmjRWPIgT/Ifq75m7eMV UsT7hRMbRsCCOqu3ALznsqLd5286nKiSefhgY5AszJIlt31AkSlAmP+wPCOw8+TXhD1SUn1hMjT 5 X-Received: by 2002:a1c:850c:: with SMTP id h12-v6mr2376015wmd.136.1527259257878; Fri, 25 May 2018 07:40:57 -0700 (PDT) X-Received: by 2002:a1c:850c:: with SMTP id h12-v6mr2375976wmd.136.1527259257067; Fri, 25 May 2018 07:40:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527259257; cv=none; d=google.com; s=arc-20160816; b=PGGuLE231RXNSMAIGqRAvW3SaoF5ba8zqkK/XruV/qxwboha3qmFBZIcUmDIbPZkb0 V7VP4ml3rxHPXobQm0pKOXh90En2OJXLezjQ75ec5bVqHxgkyl4jYEF4Oz1Smt5jyvKr 8KNUif9Byg1UqyJVu9WekBhPrdjE5zOy+6QdBxcuElgU0CzVfbc0utlRqMk/2CUwHm2e c0TWlXL8B0OmWPr5kd9vRVjY+rVKOZl8LPQggWlMMKMU954AHVgFdM7pnUMkgBxO70GC JKwqYGZZJ1FeH0/DjPkGG388Gecavnu8bTtudKCJ/FR1i35W/B/0mGoVhKhb3vg2IA+0 NMCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=q3sk7ufsfcpLhhRUqQfALsmeeTCJvnJNnm+o4i2Xa54=; b=Kenu6vOhFh0UulXmfMoKQzU7T1/agNJyN7ME1dXsWPpPgAWiQgnKoRH5bYNPwhY4s6 Ov7JFXjZNbkv9Si5YpO2UKFQPdOtKkLmlfHKWqkB+wC7i8TV+ZVP1cuvMUs2SbfJ2h+c FbiMoTIwWefKP3lVANF7BKHOqCzKkImdcg4sSQE8NZwj5fqNIFXB52sQ7axjCj+U5EEl SQB37BH7GXwRb9qH1pJ2Ad+XX5dC4ICsV1IzwsAf/EUYywjAZKcjlEdgh61XY0gWYj/Z NpS1r2DoSTJReSMDmc46wkY25kU37eGyi8I4ODAAlLE4cEFvz+0lvVQTAzbcYGXgm8Sx 09Nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tcWQxRZ/; 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 2-v6sor2461417wml.0.2018.05.25.07.40.56 for (Google Transport Security); Fri, 25 May 2018 07:40:57 -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=tcWQxRZ/; 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:content-transfer-encoding; bh=q3sk7ufsfcpLhhRUqQfALsmeeTCJvnJNnm+o4i2Xa54=; b=tcWQxRZ/mqoxsEl9EfRgjs1qbDj3KaenY10cGBtzuJ72shbgzrnR4W7hMfZgF2aWpJ iBtibVECRPMNlzucBysvVwlJqSocu6Ca9eJ5Yk8cxBcGV9bVhyHoEHLyCSDUiSrAvR7y JAY9fM+JJUHsvteJhY19bDQ5mcODEPbXdK4OCXuoXEyjIN1R6UMQO68lZrUmwweQI8/R yzGe+itQZ64Uoywgco3oUAvtJ879ZGfQsG0gM3PPt6TIzJ9VESMZN4a4KSeei3iZPASA 1bDPvtpFq+G7z6qG06nfpPOzCTBlaAw2soOCT+6Co25sMHlV3feFpacqDB7pNgZralgH E5vQ== X-Google-Smtp-Source: ADUXVKIj+Pj9/j+oaPNiMq+uy3wHo6f4hxiN/YKX8+JxM2pW1ATL4IKjZFl1OeuoEavey5aefN646Q== X-Received: by 2002:a1c:470d:: with SMTP id u13-v6mr1989624wma.56.1527259255942; Fri, 25 May 2018 07:40:55 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id q2-v6sm25293885wrm.26.2018.05.25.07.40.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 May 2018 07:40:55 -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 v2 08/16] khwasan: add tag related helper functions Date: Fri, 25 May 2018 16:40:24 +0200 Message-Id: X-Mailer: git-send-email 2.17.0.921.gf22659ad46-goog In-Reply-To: References: MIME-Version: 1.0 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/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) {