From patchwork Mon Nov 23 20:07:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 11929073 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5A272C56201 for ; Tue, 24 Nov 2020 13:50:49 +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 F062D20872 for ; Tue, 24 Nov 2020 13:50:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="CD+gh+Kb"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="dL1+HcHK"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="bVdbd24i" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F062D20872 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+linux-arm-kernel=archiver.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=RnZoJI2DC5XJUGF1Rj2Rs6pzkL7bvzlLkmaVZYtU2y0=; b=CD+gh+Kb+UtezOvUdqYYUPX4P uR/825xeu28uEIPXmAbwr/dNTEjLg90An/IjLLFhBBMYGrzML/l9J0bzuBUXxKEve9JrKxtLcRhhM 1UvVMdeUV4/Xiiyo0ysL/+23ipI5rP+rZJBiO7dz/lXiaZ1WF20YRBU0o/LUbMnfJSQk2uhGssAl0 /aAOkP3LkPnDwVEtb5K3cQq5CyzHqFcFSXrG99WROU7y8Us4m/gZxjXTDvGyr9hhh1P/OSSbW4nS+ dCay0rxBMrrnNWP5Lpfd2ZldlQIrldhaDJowinK18VX4UTlNZbZ6k5kz18laMCzARtYkAfSHTC7Up FDSvOhAkw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1khYhM-0005lI-2e; Tue, 24 Nov 2020 13:49:36 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1khIP0-0006KM-Nh for linux-arm-kernel@merlin.infradead.org; Mon, 23 Nov 2020 20:25:35 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:Cc:To:From:Subject: References:Mime-Version:Message-Id:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=sJ6lYDMOzh2s8bJA3IjWtOTOyr6/j6k+VQaTKZ4N9N8=; b=dL1+HcHKQQOeMirACfpyrOGJmi YvVxIWb+c8fTua5MtHs/vspBKiewxk6FhceAA1iqJo7m15FafrrycJBN/HdkqAZFDpZVkL+JCOowq OAFVTSbxqi4jyTsYZYhO7EI/OosPklTK0t6pmATXPHHEuRY4icEjLfUyNPXTKo8hhUmfyfSJ8i4N5 BTaMob5UX6ORxKys5dSwspiju9oriTV4pOjj9eCyosM0lCFqDNCUvp9hWFM7KF78uL2Zo2cRXEBxY JVkQ2NdJG6kizG8AlKEw2ZJyaTnO6jhOOAWFlCzGPZTbgnL5PjvdBDxVgKypnz1zZimWfqo4HNyg3 Yaino9TA==; Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by casper.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1khI9C-0005Kd-5s for linux-arm-kernel@lists.infradead.org; Mon, 23 Nov 2020 20:09:21 +0000 Received: by mail-wr1-x449.google.com with SMTP id b3so4648532wrs.6 for ; Mon, 23 Nov 2020 12:09:12 -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=sJ6lYDMOzh2s8bJA3IjWtOTOyr6/j6k+VQaTKZ4N9N8=; b=bVdbd24ijfPgFmgYtInw3m6bPGrErYcPXSyDVlmNFIWF+jMZCtEDXjkkJW/3S2IACn obXUG/JG+e5CbX7JcAAksQIpRVczgjvsnnlMJ1/l6EdkbA+0pDof1hdYm0kO33YUOTru 0aHdXeqqO6IbzYUm+7sbF6ElNU0oCMGXYtayEMpe9YqH5sCPdIfcUdar98RVZUMt9ELe URXimDGTJijKQ65pV/hO6lqDGL/qSzKSR107E3t6gdFbycaktVzcEhqiwZw5D9RvFQ6X 1jhWu/bV6sivN9Mb6hbd0ycatfRpxOf1mxs5o4JgvdnsGlZoy72PnAFUTj1xfCtGXLx1 eOtQ== 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=sJ6lYDMOzh2s8bJA3IjWtOTOyr6/j6k+VQaTKZ4N9N8=; b=tLPZ5M0gQ/CaxpIAbdT1vqhpw0kDmP4kqDdCbFWzIG40fjlR1MqfXR+mShfRM8B1ip GrLiD2zcG9ogTyU1kldHfKpo+H+aSULQiK8oCiJD8MJ6X+lGvK5jhSQvMI5x8EWk2Uor GC54JRczHjYkeapBgfmNa3fSpKLspHFoUvcwuua7AXLLVJsPXSZvs7wpmX7SQw87LEAw Cupgci+zJ1/kRqJSZvGN8f1UvQTjAKjvAmPg7h8OFwRQG+L3nQ36oB/D/37NmJAcObbp fTOH0ro/cvxrN+cWRIHxmVvDp0QAsjYio3wPwPlrRWDT9SX2kqoZoFSvBuEb75EBt7J8 tPhQ== X-Gm-Message-State: AOAM532e4zALRzvKo8JdVEQSZaWYoDz23OpR326vQxZF1nCyy9mmA+CX neC+b7sNihaKBf25V/H/GlDZlZqeZ9Dt49hh X-Google-Smtp-Source: ABdhPJze2dRqWoVk7IbesBBsgSN4J8S2K53wFcp7cQcLIFgj537k7WazZ6f2gOQC0MpbzP09Fbl1PXLAaMSazNMm X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a1c:3c44:: with SMTP id j65mr607928wma.13.1606162151581; Mon, 23 Nov 2020 12:09:11 -0800 (PST) Date: Mon, 23 Nov 2020 21:07:45 +0100 In-Reply-To: Message-Id: <5fb1ec0152bb1f521505017800387ec3e36ffe18.1606161801.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.2.454.gaff20da3a2-goog Subject: [PATCH mm v11 21/42] kasan: separate metadata_fetch_row for each mode From: Andrey Konovalov To: Andrew Morton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201123_200915_418718_F046B2D0 X-CRM114-Status: GOOD ( 20.04 ) 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: linux-arm-kernel@lists.infradead.org, Marco Elver , Catalin Marinas , Kevin Brodsky , Will Deacon , Branislav Rankov , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexander Potapenko , Evgenii Stepanov , Andrey Konovalov , Andrey Ryabinin , Vincenzo Frascino , Dmitry Vyukov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.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 Reviewed-by: Alexander Potapenko --- 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 c79d30c6fcdb..3b349a6e799d 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -58,6 +58,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; @@ -170,6 +177,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 ab28e350bf39..2c503b667413 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);