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: 932345 Return-Path: X-Original-To: incoming-imx@patchwork.ozlabs.org Delivered-To: patchwork-incoming-imx@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-arm-kernel-bounces+incoming-imx=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="s5YUIOql"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="bC/eRfe5"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 419sdD2bjFz9s4w for ; Thu, 21 Jun 2018 03:43:44 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=XMW1grxwlKVTeWmZwjbNI0BQApclc1bYCHm7Ox8qpqo=; b=s5YUIOqlXPDLA0 o9jYURPHAvHPFKmzmJfFYJwnOUyrMT8HfoLSOAz7PSerrP39R97Hbk+wbUDCp4Yz9etWFwcUYevbH yZz/7S4VHNUJxDUlh8VfcZduzRTXCW8PAlhN6YPSkh6bSS5vw9F11emMxtSTmZ8wA3Lh+8vNhnfns KchiimZ53a3WmGqH/ymUCwmHiU6CBCfisNmDerz9ks72fvSbZvUQLBYCbLpHmJdFBCqJbpTyywmyU q6msiMsGNaWb/J3kk+ox9/XfghlVSkggKdZW/PpzFKZqW1CjluXr2oPu7ipGh4yig/LN2XHOXGSeb PBGwMJ7TYfS84GKAEu2Q==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fVh8k-0003c9-NT; Wed, 20 Jun 2018 17:43:30 +0000 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fVh5j-0001qE-RK for linux-arm-kernel@lists.infradead.org; Wed, 20 Jun 2018 17:40:54 +0000 Received: by mail-wm0-x241.google.com with SMTP id 69-v6so986132wmf.3 for ; Wed, 20 Jun 2018 10:40:21 -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=kN1ATKT0SZRcvbzcHVlW4bJuxo8qUYuf0LtW/26uGbfKhjb096OKUOcmtfZiWEK/Tu 3rnerd/9042clCLvVnpNMzFd9157Vr0wz3orBQHxpTWtDG749NVd1xyvsh2Xk+lZ1J12 vRvBVALoeo6z2WPmbbTdjTVaSr/H9J5S6r8lp78mMw7XCLTMfIqRD5PXcoY4u/Du8fis FCKcPTNs7S5ZJeOtqWeCYu/W9tiZhAh2uOk2P/aKK98Yh964Sf+orxi5TaHL3cSfw9kH ybUA3l/9u4Pa1iCan+bLmRhFIP8kqHpdOxklnvXW/PoH5hvdwJ5e2SJKVir6TeLEJZan 86sQ== X-Gm-Message-State: APt69E0m1IEgYgOcLKDRVAtNuZ6QcxwhJOGLd0TNvF92bDXRpK9lEkxs 1dLpWd5H6YtOGgKFpJJ8PWkk5A== 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 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180620_104023_919737_A9F424A8 X-CRM114-Status: GOOD ( 18.09 ) X-Spam-Score: -15.6 (---------------) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-15.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:400c:c09:0:0:0:241 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -7.5 USER_IN_DEF_DKIM_WL From: address is in the default DKIM white-list -7.5 USER_IN_DEF_SPF_WL From: address is in the default SPF white-list 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.5 ENV_AND_HDR_SPF_MATCH Env and Hdr From used in default SPF WL Match -0.0 T_DKIMWL_WL_MED DKIMwl.org - Whitelisted Medium sender X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Chintan Pandya , Jacob Bramley , Jann Horn , Ruben Ayrapetyan , Andrey Konovalov , Lee Smith , Kostya Serebryany , Mark Brand , Ramana Radhakrishnan , Evgeniy Stepanov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+incoming-imx=patchwork.ozlabs.org@lists.infradead.org List-Id: linux-imx-kernel.lists.patchwork.ozlabs.org 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) {