From patchwork Tue Sep 15 21:16:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 11777749 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 ECCF76CA for ; Tue, 15 Sep 2020 21:17:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9D3B120770 for ; Tue, 15 Sep 2020 21:17:11 +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="Uqy/64yl" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9D3B120770 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 770BC900086; Tue, 15 Sep 2020 17:17:08 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7188B900012; Tue, 15 Sep 2020 17:17:08 -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 62CBD900086; Tue, 15 Sep 2020 17:17:08 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0028.hostedemail.com [216.40.44.28]) by kanga.kvack.org (Postfix) with ESMTP id 4926B900012 for ; Tue, 15 Sep 2020 17:17:08 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 0F9EB8249980 for ; Tue, 15 Sep 2020 21:17:08 +0000 (UTC) X-FDA: 77266556136.23.ray89_340663727114 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin23.hostedemail.com (Postfix) with ESMTP id DBC0F37604 for ; Tue, 15 Sep 2020 21:17:07 +0000 (UTC) X-Spam-Summary: 1,0,0,bcb2cc32a36e8a2d,d41d8cd98f00b204,3ui9hxwokcdctgwkxrdgoezhhzex.vhfebgnq-ffdotvd.hkz@flex--andreyknvl.bounces.google.com,,RULES_HIT:2:41:69:152:355:379:541:800:960:966:973:982:988:989:1042:1260:1277:1313:1314:1345:1359:1431:1437:1516:1518:1535:1593:1594:1605:1730:1747:1777:1792:1981:2194:2196:2199:2200:2393:2553:2559:2562:2693:2901:3138:3139:3140:3141:3142:3152:3865:3866:3867:3871:3872:3874:4049:4120:4250:4321:4385:4605:5007:6119:6261:6653:6742:7875:7903:8603:9165:9592:9969:10004:11026:11232:11473:11658:11914:12043:12048:12297:12438:12555:12683:12740:12895:12986:14096:14097:14394:14659:21080:21324:21365:21444:21451:21627:21740:21772:21796:21990:30003:30036:30045:30054:30075:30090,0,RBL:209.85.128.74:@flex--andreyknvl.bounces.google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100;04yfjxic3pyo8sks4a1f541698z7qyp1qhpgabnkfc6nmnx8w9aixqs9m65xrr9.pdz4fecn9gnpqyndhocuwgdgmomm8ymdj9nyaeineo1759yaswe8t5aaxxcyrg8.w-lbl8.mailshell.net-223.238.255.100,CacheIP:non e,Bayesi X-HE-Tag: ray89_340663727114 X-Filterd-Recvd-Size: 9544 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Tue, 15 Sep 2020 21:17:07 +0000 (UTC) Received: by mail-wm1-f74.google.com with SMTP id x6so256397wmi.1 for ; Tue, 15 Sep 2020 14:17:07 -0700 (PDT) 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=cNUkjw3h6h2vvdGQHstiUCBBTL6uA9taUHJKEUHsZNs=; b=Uqy/64yl/TSrssjZLH9qRL0b1Q9ojBJrOiiVPJ6ffDQyrLQwhEesqFOJzIy2SSQmB6 5Wrl7d+D7GwJr3iWjqN/g1Ny2ldoUFt/lxuop9KNLjFnQAb2HjaHQKWj7TRjUdJC2lWd hQ4u7RssH2+XZd6nSzU8L9eWNVu4lSjWsQPxXgZQGxMY/ynXM8tFY+vKO9z+j0VGLgaN u979tZVDr+n6SAG0gkOE5MPgiZXcWW7rtVKsnD6/NdRFxC5Ve11juxVWE4kEhLdnaz1x SZ7nt3Wfnj/6q1QYIc3/bAtTVIKiLtanjm1hhdDEPfwaS62q9mGK2QonxuEACBYt+zCJ zwMw== 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=cNUkjw3h6h2vvdGQHstiUCBBTL6uA9taUHJKEUHsZNs=; b=VQJGlyUmN1y2ABhaBV6tgzk8hrTLTkOpsToJMP9BFPW4b2lWJhZrLqt9QWFk9UaIOU zSbExBrkUIUp9KUdM1hvubm3+KKy0/7ZtNbbfgyVGMstR1JGTBNKzQAxXQamVUvi12ZU dLGcJABJANHY7DFjLAC4pnMZfpenwdfCMvLjw7SlFDiM/vmxUFji8c33VugGGUEBx9p4 goqPj3nK3OXNcRs38G7l6WAjCEaCDD4Sy6wCbpRvp3d9y2vvcOuZnT25BA+izbA0Qyvy 1Dp+c7vMwhhJ0f7DoCVQ6IG9QgjjElVMT1TrEui3K1JxK+0E1DM3G0lo8/Qsd6JZOER+ +urg== X-Gm-Message-State: AOAM531oUGwvKxCg3Gb3Vl7iF6uNW0KSF8SpgLZPIVAKVAKcnt//s4oc w+VTy31FWEE50g9W92XqdqikKz/NhnExoLcy X-Google-Smtp-Source: ABdhPJzL8BhYNRzK4slQohtZ+7TSTK/+JbYrZ5VcU+mY7HWwBH8wrvRTXucYiWwyPZaxtMd5XV7Uyp2JR6WDFw6Q X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a05:600c:245:: with SMTP id 5mr1264364wmj.33.1600204626209; Tue, 15 Sep 2020 14:17:06 -0700 (PDT) Date: Tue, 15 Sep 2020 23:16:00 +0200 In-Reply-To: Message-Id: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.28.0.618.gf4bc123cb7-goog Subject: [PATCH v2 18/37] kasan: separate metadata_fetch_row for each mode From: Andrey Konovalov To: Dmitry Vyukov , Vincenzo Frascino , Catalin Marinas , kasan-dev@googlegroups.com Cc: Andrey Ryabinin , Alexander Potapenko , Marco Elver , Evgenii Stepanov , Elena Petrova , Branislav Rankov , Kevin Brodsky , Will Deacon , Andrew Morton , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov X-Rspamd-Queue-Id: DBC0F37604 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam03 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. Rework print_memory_metadata() to make it agnostic with regard to the way metadata is stored. Allow providing a separate metadata_fetch_row() implementation for each KASAN mode. Hardware tag-based KASAN will provide its own implementation that doesn't use shadow memory. No functional changes for software modes. Signed-off-by: Andrey Konovalov Signed-off-by: Vincenzo Frascino --- Change-Id: I5b0ed1d079ea776e620beca6a529a861e7dced95 --- mm/kasan/kasan.h | 8 ++++++ mm/kasan/report.c | 56 +++++++++++++++++++-------------------- mm/kasan/report_generic.c | 5 ++++ mm/kasan/report_tags.c | 5 ++++ 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 38fa4c202e9a..1d3c7c6ce771 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -56,6 +56,13 @@ #define KASAN_ABI_VERSION 1 #endif +/* Metadata layout customization. */ +#define META_BYTES_PER_BLOCK 1 +#define META_BLOCKS_PER_ROW 16 +#define META_BYTES_PER_ROW (META_BLOCKS_PER_ROW * META_BYTES_PER_BLOCK) +#define META_MEM_BYTES_PER_ROW (META_BYTES_PER_ROW * KASAN_GRANULE_SIZE) +#define META_ROWS_AROUND_ADDR 2 + struct kasan_access_info { const void *access_addr; const void *first_bad_addr; @@ -167,6 +174,7 @@ 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); +void metadata_fetch_row(char *buffer, void *row); #ifdef CONFIG_KASAN_STACK_ENABLE void print_address_stack_frame(const void *addr); diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 6306673e7062..c904edab33b8 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -36,12 +36,6 @@ #include "kasan.h" #include "../slab.h" -/* Metadata layout customization. */ -#define META_BYTES_PER_BLOCK 1 -#define META_BLOCKS_PER_ROW 16 -#define META_BYTES_PER_ROW (META_BLOCKS_PER_ROW * META_BYTES_PER_BLOCK) -#define META_ROWS_AROUND_ADDR 2 - static unsigned long kasan_flags; #define KASAN_BIT_REPORTED 0 @@ -241,55 +235,59 @@ static void print_address_description(void *addr, u8 tag) print_address_stack_frame(addr); } -static bool row_is_guilty(const void *row, const void *guilty) +static bool meta_row_is_guilty(const void *row, const void *addr) { - return (row <= guilty) && (guilty < row + META_BYTES_PER_ROW); + return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW); } -static int shadow_pointer_offset(const void *row, const void *shadow) +static int meta_pointer_offset(const void *row, const void *addr) { - /* The length of ">ff00ff00ff00ff00: " is - * 3 + (BITS_PER_LONG/8)*2 chars. + /* + * Memory state around the buggy address: + * ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe + * ... + * + * The length of ">ff00ff00ff00ff00: " is + * 3 + (BITS_PER_LONG / 8) * 2 chars. + * The length of each granule metadata is 2 bytes + * plus 1 byte for space. */ - return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 + - (shadow - row) / META_BYTES_PER_BLOCK + 1; + return 3 + (BITS_PER_LONG / 8) * 2 + + (addr - row) / KASAN_GRANULE_SIZE * 3 + 1; } static void print_memory_metadata(const void *addr) { int i; - const void *shadow = kasan_mem_to_shadow(addr); - const void *shadow_row; + void *row; - shadow_row = (void *)round_down((unsigned long)shadow, - META_BYTES_PER_ROW) - - META_ROWS_AROUND_ADDR * META_BYTES_PER_ROW; + row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW) + - META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW; pr_err("Memory state around the buggy address:\n"); for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) { - const void *kaddr = kasan_shadow_to_mem(shadow_row); - char buffer[4 + (BITS_PER_LONG/8)*2]; - char shadow_buf[META_BYTES_PER_ROW]; + char buffer[4 + (BITS_PER_LONG / 8) * 2]; + char metadata[META_BYTES_PER_ROW]; snprintf(buffer, sizeof(buffer), - (i == 0) ? ">%px: " : " %px: ", kaddr); + (i == 0) ? ">%px: " : " %px: ", row); + /* * We should not pass a shadow pointer to generic * function, because generic functions may try to * access kasan mapping for the passed address. */ - memcpy(shadow_buf, shadow_row, META_BYTES_PER_ROW); + metadata_fetch_row(&metadata[0], row); + print_hex_dump(KERN_ERR, buffer, DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1, - shadow_buf, META_BYTES_PER_ROW, 0); + metadata, META_BYTES_PER_ROW, 0); - if (row_is_guilty(shadow_row, shadow)) - pr_err("%*c\n", - shadow_pointer_offset(shadow_row, shadow), - '^'); + if (meta_row_is_guilty(row, addr)) + pr_err("%*c\n", meta_pointer_offset(row, addr), '^'); - shadow_row += META_BYTES_PER_ROW; + row += META_MEM_BYTES_PER_ROW; } } diff --git a/mm/kasan/report_generic.c b/mm/kasan/report_generic.c index 29d30fae9421..6524651b5d2e 100644 --- a/mm/kasan/report_generic.c +++ b/mm/kasan/report_generic.c @@ -127,6 +127,11 @@ const char *get_bug_type(struct kasan_access_info *info) return get_wild_bug_type(info); } +void metadata_fetch_row(char *buffer, void *row) +{ + memcpy(buffer, kasan_mem_to_shadow(row), META_BYTES_PER_ROW); +} + #ifdef CONFIG_KASAN_STACK_ENABLE static bool __must_check tokenize_frame_descr(const char **frame_descr, char *token, size_t max_tok_len, diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 6ddb55676a7c..4060d0503462 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -85,6 +85,11 @@ void *find_first_bad_addr(void *addr, size_t size) return p; } +void metadata_fetch_row(char *buffer, void *row) +{ + memcpy(buffer, kasan_mem_to_shadow(row), META_BYTES_PER_ROW); +} + void print_tags(u8 addr_tag, const void *addr) { u8 *shadow = (u8 *)kasan_mem_to_shadow(addr);