From patchwork Fri Aug 14 17:27:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 11715111 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DC6E616B1 for ; Fri, 14 Aug 2020 17:36:56 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id AE5F520768 for ; Fri, 14 Aug 2020 17:36:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="WzEpFNH6"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="I7BFJRn9" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AE5F520768 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=lcjaXJNCJJuLHHey063Xqtvd8c0xoVeOdj1yDh4bExI=; b=WzEpFNH6N/3Q4wDdCGBYooIcJ B2zW8p1Y9ZXkD08INZ8LcCf6ZJTNR1H2NS11H2Cul5zxevjmtRbTzqMRAoeSUBvS35zjw/Dw3WSMQ rkk8g/S95KZe5qqtKBb3yurHpKq8Ivr7VqD16ofAkJWM8IRyT2ldIPyTaO/wb/c/MqcSqYgiEeWJq UW99lKT+U7AQBh4TaLR1d7CMetc0fuI/MH8ikCeu1WhJW+tUjfZq80Y1VJ2+XsNwpkUvoLIjPqNoR 3NW1aMVlt2iC0fdhfqAfK59y6V4LGZURn1h91uVlzOCoMzMDTgOUz4+m5N5h/Qki9ICA2RwFhCsLE 4PESDgj+w==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k6dd1-0000MT-G3; Fri, 14 Aug 2020 17:36:31 +0000 Received: from mail-wr1-x44a.google.com ([2a00:1450:4864:20::44a]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1k6dVU-0003nA-Ri for linux-arm-kernel@lists.infradead.org; Fri, 14 Aug 2020 17:28:57 +0000 Received: by mail-wr1-x44a.google.com with SMTP id t12so3615910wrp.0 for ; Fri, 14 Aug 2020 10:28:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=kJlHQlyHf1HvTYFCcN+LwDe0IIk6i9RqNd5dGciZ6Z0=; b=I7BFJRn9BbumelTeTM7IKHGPfBZcynJd43gIcFrt5XEcvzxy0Wzf8WnsNKaTIax5jr HUvBgUpYNKNWZYu+bURQKRH5n06/cdNWba7CPxSKJgV5qIs+RECFgmKBeRLYIxOLAZCG 1dKItAvdJfm8oSnX6tKqt5qngTIxJJ7zST11LP47f1lDcYJY1GwVjpWIpmjB6qNRDmvb Ch05VqLHv7VZA2+ppDka6QFrspWllhq5h62z64FvDAzfq6S81KVTNScwgvfSyGN56MO2 HR5lZx+yeJC57vDA/Fn4Jf2BN3rzo6Jo4txHmxqoaqx/w6IO/faSFD8SdIfaeg0DU1gH LoNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=kJlHQlyHf1HvTYFCcN+LwDe0IIk6i9RqNd5dGciZ6Z0=; b=XblMqpnHBVGl8tXoOsJqAkBYfygxaSx9zr4nZm7/+YM8jmNQNVd5f0CtLPEJxWLg70 TQEgHvqcJQzypL0AFny6g5rEetWP3DMZ1qOWTyH5Wb+oriKzKkWWCSRGKuw/ggsfOcgt RPIuyEjz96QYA032IJw7w367I/7Cm0+Dlu+kjewaJeVXjEzzAniLbSgLSYuP3343IGz8 hDNauf5WaaXLzAFiXOZtteCRCAROfG7Wi5rasZWgHKzhRAjyr18QHDyHZ+q2WAjJ1Tch zOthB8C/uGUiu0eq13wF0U950kxW577jms22qDo8cvYB6QWo4JfRUsME6iNzaCOlFi4S RM9g== X-Gm-Message-State: AOAM530pnt3mtBea8vmnVw2UD2rUSl8CGR/nLtwWxz1CFOu2mFELMeDJ 9JqbBbd8B3PNNTIZ5X2KOUnKH7JEojfcU/ch X-Google-Smtp-Source: ABdhPJxqdUAPjARnbQ5lX0fWboRK95ldEyr+27Agji2Z4xPsor/NlyQqlIQmOJATBl8+Gaf9vkUxDnSSxUMU/xLW X-Received: by 2002:a1c:2e4e:: with SMTP id u75mr3382777wmu.134.1597426121790; Fri, 14 Aug 2020 10:28:41 -0700 (PDT) Date: Fri, 14 Aug 2020 19:27:15 +0200 In-Reply-To: Message-Id: <8384a6b24203b5719ef4f3a0339f740ad3299e9c.1597425745.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.28.0.220.ged08abb693-goog Subject: [PATCH 33/35] kasan, slub: reset tags when accessing metadata From: Andrey Konovalov To: Dmitry Vyukov , Vincenzo Frascino , Catalin Marinas , kasan-dev@googlegroups.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200814_132844_993633_3487C778 X-CRM114-Status: GOOD ( 17.19 ) X-Spam-Score: -7.7 (-------) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-7.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:44a 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 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 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_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.0 DKIMWL_WL_MED DKIMwl.org - Medium sender X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Marco Elver , Elena Petrova , Andrey Konovalov , Kevin Brodsky , Will Deacon , Branislav Rankov , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexander Potapenko , linux-arm-kernel@lists.infradead.org, Andrey Ryabinin , Andrew Morton , Evgenii Stepanov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org SLUB allocator accesses metadata for slab objects, that may lie out-of-bounds of the object itself, or be accessed when an object is freed. Handle this for Hardware tag-based KASAN by resetting tags when accessing metadata. Hardware tag-based KASAN doesn't rely on metadata_access_disable/enable(), and therefore requires resetting tags in the sections of code guarded by those annotations. Signed-off-by: Andrey Konovalov --- mm/page_poison.c | 2 +- mm/slub.c | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/mm/page_poison.c b/mm/page_poison.c index 34b9181ee5d1..d90d342a391f 100644 --- a/mm/page_poison.c +++ b/mm/page_poison.c @@ -43,7 +43,7 @@ static void poison_page(struct page *page) /* KASAN still think the page is in-use, so skip it. */ kasan_disable_current(); - memset(addr, PAGE_POISON, PAGE_SIZE); + memset(kasan_reset_tag(addr), PAGE_POISON, PAGE_SIZE); kasan_enable_current(); kunmap_atomic(addr); } diff --git a/mm/slub.c b/mm/slub.c index ef303070d175..a786e1cee095 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -249,7 +249,7 @@ static inline void *freelist_ptr(const struct kmem_cache *s, void *ptr, { #ifdef CONFIG_SLAB_FREELIST_HARDENED /* - * When CONFIG_KASAN_SW_TAGS is enabled, ptr_addr might be tagged. + * When CONFIG_KASAN_SW/HW_TAGS is enabled, ptr_addr might be tagged. * Normally, this doesn't cause any issues, as both set_freepointer() * and get_freepointer() are called with a pointer with the same tag. * However, there are some issues with CONFIG_SLUB_DEBUG code. For @@ -275,6 +275,7 @@ static inline void *freelist_dereference(const struct kmem_cache *s, static inline void *get_freepointer(struct kmem_cache *s, void *object) { + object = kasan_reset_tag(object); return freelist_dereference(s, object + s->offset); } @@ -304,6 +305,7 @@ static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp) BUG_ON(object == fp); /* naive detection of double free or corruption */ #endif + freeptr_addr = (unsigned long)kasan_reset_tag((void *)freeptr_addr); *(void **)freeptr_addr = freelist_ptr(s, fp, freeptr_addr); } @@ -546,8 +548,8 @@ static void print_section(char *level, char *text, u8 *addr, unsigned int length) { metadata_access_enable(); - print_hex_dump(level, text, DUMP_PREFIX_ADDRESS, 16, 1, addr, - length, 1); + print_hex_dump(level, kasan_reset_tag(text), DUMP_PREFIX_ADDRESS, + 16, 1, addr, length, 1); metadata_access_disable(); } @@ -578,7 +580,7 @@ static struct track *get_track(struct kmem_cache *s, void *object, p = object + get_info_end(s); - return p + alloc; + return kasan_reset_tag(p + alloc); } static void set_track(struct kmem_cache *s, void *object, @@ -591,7 +593,8 @@ static void set_track(struct kmem_cache *s, void *object, unsigned int nr_entries; metadata_access_enable(); - nr_entries = stack_trace_save(p->addrs, TRACK_ADDRS_COUNT, 3); + nr_entries = stack_trace_save(kasan_reset_tag(p->addrs), + TRACK_ADDRS_COUNT, 3); metadata_access_disable(); if (nr_entries < TRACK_ADDRS_COUNT) @@ -755,7 +758,7 @@ static __printf(3, 4) void slab_err(struct kmem_cache *s, struct page *page, static void init_object(struct kmem_cache *s, void *object, u8 val) { - u8 *p = object; + u8 *p = kasan_reset_tag(object); if (s->flags & SLAB_RED_ZONE) memset(p - s->red_left_pad, val, s->red_left_pad); @@ -785,7 +788,7 @@ static int check_bytes_and_report(struct kmem_cache *s, struct page *page, u8 *addr = page_address(page); metadata_access_enable(); - fault = memchr_inv(start, value, bytes); + fault = memchr_inv(kasan_reset_tag(start), value, bytes); metadata_access_disable(); if (!fault) return 1; @@ -881,7 +884,7 @@ static int slab_pad_check(struct kmem_cache *s, struct page *page) pad = end - remainder; metadata_access_enable(); - fault = memchr_inv(pad, POISON_INUSE, remainder); + fault = memchr_inv(kasan_reset_tag(pad), POISON_INUSE, remainder); metadata_access_disable(); if (!fault) return 1; @@ -1126,7 +1129,7 @@ void setup_page_debug(struct kmem_cache *s, struct page *page, void *addr) return; metadata_access_enable(); - memset(addr, POISON_INUSE, page_size(page)); + memset(kasan_reset_tag(addr), POISON_INUSE, page_size(page)); metadata_access_disable(); } @@ -2816,10 +2819,10 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, stat(s, ALLOC_FASTPATH); } - maybe_wipe_obj_freeptr(s, object); + maybe_wipe_obj_freeptr(s, kasan_reset_tag(object)); if (unlikely(slab_want_init_on_alloc(gfpflags, s)) && object) - memset(object, 0, s->object_size); + memset(kasan_reset_tag(object), 0, s->object_size); slab_post_alloc_hook(s, gfpflags, 1, &object);