From patchwork Tue Nov 10 22:10:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 11895531 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 859DF138B for ; Tue, 10 Nov 2020 22:11:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4169B20637 for ; Tue, 10 Nov 2020 22:11:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="eiTOEKy/" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4169B20637 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 927826B0093; Tue, 10 Nov 2020 17:11:40 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8D7746B0095; Tue, 10 Nov 2020 17:11:40 -0500 (EST) 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 7EF236B0096; Tue, 10 Nov 2020 17:11:40 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0008.hostedemail.com [216.40.44.8]) by kanga.kvack.org (Postfix) with ESMTP id 527DD6B0093 for ; Tue, 10 Nov 2020 17:11:40 -0500 (EST) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id E33EE181AEF15 for ; Tue, 10 Nov 2020 22:11:39 +0000 (UTC) X-FDA: 77469906318.29.thumb15_2b12792272f8 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin29.hostedemail.com (Postfix) with ESMTP id AB46418086CD8 for ; Tue, 10 Nov 2020 22:11:39 +0000 (UTC) X-Spam-Summary: 1,0,0,8bf7a79855984f35,d41d8cd98f00b204,3grcrxwokcogkxnboiuxfvqyyqvo.mywvsxeh-wwufkmu.ybq@flex--andreyknvl.bounces.google.com,,RULES_HIT:41:69:152:355:379:541:800:960:965:966:968:973:988:989:1042:1260:1277:1313:1314:1345:1359:1431:1437:1516:1518:1535:1543:1593:1594:1711:1730:1747:1777:1792:1981:2194:2196:2199:2200:2393:2559:2562:3138:3139:3140:3141:3142:3152:3355:3865:3866:3870:3871:4118:4250:4321:4385:4390:4395:4605:5007:6261:6653:6742:8603:9592:9969:10004:10400:10450:10455:11026:11232:11473:11658:11914:12043:12296:12297:12438:12555:12895:12986:14181:14394:14659:14721:19904:19999:21080:21324:21365:21444:21451:21627:21772:21990:30003:30012:30054:30070:30075,0,RBL:209.85.222.202:@flex--andreyknvl.bounces.google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100;04yfeadkjw75h5go4sw68d1p4tn8qypy5pow3utkfuttq979ue8hwxotn4rj8e6.7339wnb5d4embongt14gwrwu8m6485u3m11bobxyshhnr91sx1gjr6npejwf5ii.r-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netc heck:non X-HE-Tag: thumb15_2b12792272f8 X-Filterd-Recvd-Size: 7183 Received: from mail-qk1-f202.google.com (mail-qk1-f202.google.com [209.85.222.202]) by imf48.hostedemail.com (Postfix) with ESMTP for ; Tue, 10 Nov 2020 22:11:38 +0000 (UTC) Received: by mail-qk1-f202.google.com with SMTP id c18so180047qkl.15 for ; Tue, 10 Nov 2020 14:11:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=j+TFieADiUKw2x5AzsTIkkuZWnBD6LGUhFcNfxhuTZM=; b=eiTOEKy/WTV/0ei9edEemaxrG0G2aEDaVSOg4pVbaV/t/2S+TGdpiQUbKclXGHmx/n QfHLGs+Wh3pSixMt/Cq5AtweTGqt4GGjos7sosP94vQHF8eohzH0O0NGx9TLUlNI6hWD 7Cb25cxJPMnKsDJbQ9XcnWlcdP7op053k3uFtmW3ZmgBtx5gPQFMlqryyy8i1iuw2WQr IjUkyTppd1XSrly0gHBHTjvhAgz9GP0RGUj4kYus4QJlJ3G9M951JoSwsgfTI2U+aRSK mBx0DlrzQiClHVRNrd2FZTqx7T0sIMF6d8XzayKZyTBvC1le6tIwmz8CItppjdqlp+IV IX0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=j+TFieADiUKw2x5AzsTIkkuZWnBD6LGUhFcNfxhuTZM=; b=Jbl04WJojGkUpPcQy4g2hRUql0TEBLxCNxb27enT/I7xl1RFyhXbj4F3nOOKvMAEzL f9HBgJx2pOU0vnExOLcMMYKnPUysxALb7npRP2KLOnZ1VSVVpw6cMwYtPp54q8I1LjoN 5GIWJ+BLhF1vKDSaR6+Zjdp0okMSXq7jo1Pjkm+Pu/Dc4mrX/1Sa6gy8EcY9gAEv3ok3 brYedHAEvO5BHPVRfC7hZ0hdcp2amdhTAkGBl/R7XRub1VlDGoqc9lW9MOB4uqSFUpYU GNhGsNvVRL+6r0GxIPIyPymlGnxj599Kgihb0mi8ru+1iu0u87vANTL+QPvCXf6qYXGm S2mg== X-Gm-Message-State: AOAM533Ga9PP9H5MGDRFKfkJSIOgqApQ2R+iUCQACzKw4nrUZVVeQLjs zeEPBHeP92QEANb4lmjJNED43g7WEx8WKg9a X-Google-Smtp-Source: ABdhPJynnsUk9Ry6ZH1QO07zxg7gKlnT2InfvMy6SH7iqAnb0prFcLLUgYadYtV6wUedPwJ1A7WC72O5f7THl3qT X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a05:6214:1507:: with SMTP id e7mr21116424qvy.50.1605046297470; Tue, 10 Nov 2020 14:11:37 -0800 (PST) Date: Tue, 10 Nov 2020 23:10:10 +0100 In-Reply-To: Message-Id: <427d46e86c81f3ec77147b0ade4bd551d878cf7a.1605046192.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.2.222.g5d2a92d10f8-goog Subject: [PATCH v9 13/44] kasan: hide invalid free check implementation From: Andrey Konovalov To: Catalin Marinas Cc: Will Deacon , Vincenzo Frascino , Dmitry Vyukov , Andrey Ryabinin , Alexander Potapenko , Marco Elver , Evgenii Stepanov , Branislav Rankov , Kevin Brodsky , Andrew Morton , kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov 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: This is a preparatory commit for the upcoming addition of a new hardware tag-based (MTE-based) KASAN mode. For software KASAN modes the check is based on the value in the shadow memory. Hardware tag-based KASAN won't be using shadow, so hide the implementation of the check in check_invalid_free(). Also simplify the code for software tag-based mode. No functional changes for software modes. Signed-off-by: Andrey Konovalov Signed-off-by: Vincenzo Frascino Reviewed-by: Marco Elver Reviewed-by: Alexander Potapenko --- Change-Id: I5fae9531c9fc948eb4d4e0c589744032fc5a0789 --- mm/kasan/common.c | 19 +------------------ mm/kasan/generic.c | 7 +++++++ mm/kasan/kasan.h | 2 ++ mm/kasan/sw_tags.c | 9 +++++++++ 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 123abfb760d4..543e6bf2168f 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -272,25 +272,9 @@ void * __must_check kasan_init_slab_obj(struct kmem_cache *cache, return (void *)object; } -static inline bool shadow_invalid(u8 tag, s8 shadow_byte) -{ - if (IS_ENABLED(CONFIG_KASAN_GENERIC)) - return shadow_byte < 0 || - shadow_byte >= KASAN_GRANULE_SIZE; - - /* else CONFIG_KASAN_SW_TAGS: */ - if ((u8)shadow_byte == KASAN_TAG_INVALID) - return true; - if ((tag != KASAN_TAG_KERNEL) && (tag != (u8)shadow_byte)) - return true; - - return false; -} - static bool __kasan_slab_free(struct kmem_cache *cache, void *object, unsigned long ip, bool quarantine) { - s8 shadow_byte; u8 tag; void *tagged_object; unsigned long rounded_up_size; @@ -309,8 +293,7 @@ static bool __kasan_slab_free(struct kmem_cache *cache, void *object, if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU)) return false; - shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(object)); - if (shadow_invalid(tag, shadow_byte)) { + if (check_invalid_free(tagged_object)) { kasan_report_invalid_free(tagged_object, ip); return true; } diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index ec4417156943..e1af3b6c53b8 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -187,6 +187,13 @@ bool check_memory_region(unsigned long addr, size_t size, bool write, return check_memory_region_inline(addr, size, write, ret_ip); } +bool check_invalid_free(void *addr) +{ + s8 shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(addr)); + + return shadow_byte < 0 || shadow_byte >= KASAN_GRANULE_SIZE; +} + void kasan_cache_shrink(struct kmem_cache *cache) { quarantine_remove_cache(cache); diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 1865bb92d47a..3eff57e71ff5 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -164,6 +164,8 @@ void kasan_poison_memory(const void *address, size_t size, u8 value); bool check_memory_region(unsigned long addr, size_t size, bool write, unsigned long ret_ip); +bool check_invalid_free(void *addr); + void *find_first_bad_addr(void *addr, size_t size); const char *get_bug_type(struct kasan_access_info *info); diff --git a/mm/kasan/sw_tags.c b/mm/kasan/sw_tags.c index 4bdd7dbd6647..b2638c2cd58a 100644 --- a/mm/kasan/sw_tags.c +++ b/mm/kasan/sw_tags.c @@ -121,6 +121,15 @@ bool check_memory_region(unsigned long addr, size_t size, bool write, return true; } +bool check_invalid_free(void *addr) +{ + u8 tag = get_tag(addr); + u8 shadow_byte = READ_ONCE(*(u8 *)kasan_mem_to_shadow(reset_tag(addr))); + + return (shadow_byte == KASAN_TAG_INVALID) || + (tag != KASAN_TAG_KERNEL && tag != shadow_byte); +} + #define DEFINE_HWASAN_LOAD_STORE(size) \ void __hwasan_load##size##_noabort(unsigned long addr) \ { \