Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp3410404pxx; Mon, 2 Nov 2020 08:10:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJwjG055cggtPHBtdfOaBXJ4U/PBakcevOt9mTVsLQHZrmqcoc8TIDGJFulSL/NgaAKbXvkO X-Received: by 2002:a50:dec5:: with SMTP id d5mr17640589edl.362.1604333421854; Mon, 02 Nov 2020 08:10:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604333421; cv=none; d=google.com; s=arc-20160816; b=QG1RjCsa2XueSt9jsFoUbcu3vwPcmxeHPvZnquvGxPx1rlrfk1GN/2X2wLnFoEBA4l czCuHxHcSJH+0VN5nG1DxjA0LY2SX1IRoFJbyPmt+JqZhWPEXgxhr3BwZnEkdw6kKpLL Iky0ekLsWXJF0W3erFOkn27Gc5g6w/+Lzw2LmUFLCVvzSb/g5zhh99XUieErkF2IE71C 5yE6XPSH/42m6mn5JvwA57np8OzcO/bPvLC2U0kfMkVX1bigK+W1US7AZy4sMhs/cCF+ L+F+La0oIq51uLxIlHml9b4zcFWGdZtq6KtzLm1BUePUlP5ue5Yusat0gvuY06jkuc9D ukBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=EDxSkOG22JFejpo0qUQc1PhNbOVGn2/xi+IwiijzuHU=; b=PWd+o7sr23M0s5VhzFh+44Y7SGrSLl2W5QyjL93Z6aF8+eDMMGvluqhW2Ayi5TDdfl POkySTTzOTjIVpyWzu3R6nzZMejF3GZ1SFwnJVsxG9ILmk8FGfAEJf80Ht0lSeV1I/T1 swp4dUjSt2i8FMv0FcgSBQ61KiaeFMjxJgx/cPSV84GcsmY8g3M6aYp37+H64CCiAbDa 3qg7hKWTpPBUVZdZF2jpJbjXsJi6BD8R9IfyF4Tsz25xtML1rfJgUuqCY4dnjzDs8H1c 2BLpYhggF/v+CHUwDaInukyaciXHLb8A+YOJ7H+UA/IBjnJdekNdnCuDOdukmEVEWhU/ tDGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=RUPRtoeY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k25si12474511ejk.65.2020.11.02.08.09.59; Mon, 02 Nov 2020 08:10:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=RUPRtoeY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726702AbgKBQFo (ORCPT + 99 others); Mon, 2 Nov 2020 11:05:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727126AbgKBQFm (ORCPT ); Mon, 2 Nov 2020 11:05:42 -0500 Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6A50C0617A6 for ; Mon, 2 Nov 2020 08:05:40 -0800 (PST) Received: by mail-wr1-x44a.google.com with SMTP id 11so5507869wrc.3 for ; Mon, 02 Nov 2020 08:05:40 -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=EDxSkOG22JFejpo0qUQc1PhNbOVGn2/xi+IwiijzuHU=; b=RUPRtoeY+O2qkHA+O6Af+Io1sOVR+tc8ZkOTW4uePx2ZRIY8FnvM2lRlTRr8qRfvnI LkfAjrlouEOoiVRKpokdRQnup2hYEm+irJqFKkEiAf65XS7/8qORp5EdX9DQivbmLNmF wGau9fZ5DEVQAxLL49E6VjniOykm/h7gxlINxUiUN1vpUIJDTsh6cswBRiCAmC1wqxEY gY/YdV2kWXautxxdl9hui9mYw30C7nLqw058UJlexRxTQXfEqavb4y2M8pOpCqezDVOO Jdgo73dH48BmyN9OwZ5hNT+Ta0A/prU4IuXhuM+RB+62o1kbKh3Th4vTUCvZkiIkUljM kMtA== 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=EDxSkOG22JFejpo0qUQc1PhNbOVGn2/xi+IwiijzuHU=; b=NsiH8gN5SRSU4du0ugdXZcZptO2/tPREGRAqLpowXOwFV2hK8vAewg7i9l9kj3klYf JmtZMrs5WicVTKIDropAJlPlLg6pmCc6I0lWu2sI/HCeKWvoyXDSFGaYsjdgzMUcvOlf qxj72rOmdyNYnNm5zjAsrqHUYokfnFG7mLh612Ejj1KIwn7szcIzEyff+wpodel1mRD/ 5mJ5aXTHwlbUW2YMM6BOZhUaYlBSws0SLau6lE+5ZQA8e/TeUvRrmwNQ4pNFYPZ5K3jd e4g1GwCZbJY35maUyi3BTCoxLW4/uJ90IY+2Re3H2JP4eRpDh5W4I57uApgH4ffo/p0V N4qA== X-Gm-Message-State: AOAM531KcotBLMroGZIXtp2kvxV1gTG8qk3EG+PrxvyKLrA593JE8xkr GBIGH4ri8m+RHw/Vv+h9j9YfE/UYpdehQ6yp Sender: "andreyknvl via sendgmr" X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a7b:c305:: with SMTP id k5mr19135735wmj.102.1604333139387; Mon, 02 Nov 2020 08:05:39 -0800 (PST) Date: Mon, 2 Nov 2020 17:04:10 +0100 In-Reply-To: Message-Id: <96863d2c75b58de04f0bf599ed87e05e8afd7e59.1604333009.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog Subject: [PATCH v7 30/41] kasan: separate metadata_fetch_row for each mode From: Andrey Konovalov To: Catalin Marinas , Will Deacon Cc: Vincenzo Frascino , kasan-dev@googlegroups.com, Dmitry Vyukov , Andrey Ryabinin , Alexander Potapenko , Marco Elver , Evgenii Stepanov , Elena Petrova , Branislav Rankov , Kevin Brodsky , Andrew Morton , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Reviewed-by: Marco Elver --- Change-Id: I5b0ed1d079ea776e620beca6a529a861e7dced95 --- mm/kasan/kasan.h | 8 ++++++ mm/kasan/report.c | 56 +++++++++++++++++++-------------------- mm/kasan/report_generic.c | 5 ++++ mm/kasan/report_sw_tags.c | 5 ++++ 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index ce335009aad0..e3cd6a3d2b23 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -57,6 +57,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; @@ -168,6 +175,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); #if defined(CONFIG_KASAN_GENERIC) && CONFIG_KASAN_STACK void print_address_stack_frame(const void *addr); diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 8c588588c88f..8afc1a6ab202 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -33,12 +33,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 @@ -238,55 +232,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 16ed550850e9..8a9c889872da 100644 --- a/mm/kasan/report_generic.c +++ b/mm/kasan/report_generic.c @@ -123,6 +123,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); +} + #if CONFIG_KASAN_STACK static bool __must_check tokenize_frame_descr(const char **frame_descr, char *token, size_t max_tok_len, diff --git a/mm/kasan/report_sw_tags.c b/mm/kasan/report_sw_tags.c index c87d5a343b4e..add2dfe6169c 100644 --- a/mm/kasan/report_sw_tags.c +++ b/mm/kasan/report_sw_tags.c @@ -80,6 +80,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); -- 2.29.1.341.ge80a0c044ae-goog