From patchwork Thu Mar 23 15:44:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Ryabinin X-Patchwork-Id: 95912 Delivered-To: patch@linaro.org Received: by 10.140.89.233 with SMTP id v96csp797224qgd; Thu, 23 Mar 2017 08:44:40 -0700 (PDT) X-Received: by 10.98.159.82 with SMTP id g79mr3738798pfe.189.1490283880083; Thu, 23 Mar 2017 08:44:40 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s4si4313017pfb.116.2017.03.23.08.44.39; Thu, 23 Mar 2017 08:44:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@virtuozzo.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935421AbdCWPoj (ORCPT + 17 others); Thu, 23 Mar 2017 11:44:39 -0400 Received: from mail-db5eur01on0134.outbound.protection.outlook.com ([104.47.2.134]:40832 "EHLO EUR01-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753202AbdCWPog (ORCPT ); Thu, 23 Mar 2017 11:44:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=bE2w/6GCoTSayu3KdwLdHy4AaAvOlCD7vSr6CZDeY40=; b=E4bdcrJIycek8RKoNQdMcPKK+wnfAPxx9zo4adq4A1ZbC0ABdp7JYtHfxdJsWKrABIi6I9vjrhpHkzE2qGKXIZc7HnkT+ey1eJGnPGY00OnTWcDx9y73Px4uXHdqR9Q5Q3EGI74hrPcnpHJu0Hmrqrcb2sRH3m/94ir7Vc0JMng= Authentication-Results: linux-foundation.org; dkim=none (message not signed) header.d=none; linux-foundation.org; dmarc=none action=none header.from=virtuozzo.com; Received: from localhost.sw.ru (195.214.232.6) by VI1PR0801MB2062.eurprd08.prod.outlook.com (10.173.74.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.977.11; Thu, 23 Mar 2017 15:44:31 +0000 From: Andrey Ryabinin To: Andrew Morton CC: Andrey Konovalov , Mark Rutland , Alexander Potapenko , Dmitry Vyukov , , , , Andrey Ryabinin Subject: [PATCH v3] kasan: report only the first error by default Date: Thu, 23 Mar 2017 18:44:16 +0300 Message-ID: <20170323154416.30257-1-aryabinin@virtuozzo.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20170322160647.32032-1-aryabinin@virtuozzo.com> References: <20170322160647.32032-1-aryabinin@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: DB6PR0501CA0030.eurprd05.prod.outlook.com (10.168.78.144) To VI1PR0801MB2062.eurprd08.prod.outlook.com (10.173.74.147) X-MS-Office365-Filtering-Correlation-Id: 896d8d81-5eb2-4dc6-c712-08d472038009 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:VI1PR0801MB2062; X-Microsoft-Exchange-Diagnostics: 1; VI1PR0801MB2062; 3:8TpGurIfRwmQEw53ZQ7WNKeXYqOVQ6RSS5A47Bwmb3DeRqlKEbaqjfhwdpTp+DCGP+EZSmUJFEjbGooen89PpzFkw405XjT8lmuHS+lR8Hmx9+FybOE2Xe16Jonq3gIjVPDO0PmLI7w4M9LlTX9i2CZko3WZbuvms7BXa1mJIFzepzQSaPM22jwhGt8QZiFTXrYX6ftIv6l/5prKh8ITdBESPzNLiTrfmpmI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6ILnLOlAEdQFL4urEdSIJCQan4ACIfGaiPLwBT7Pca/ZUpjxCrhwqVCjSwUokMq17qC/EHJr4PE/FQRL9ay/8sd0uzsGmeiZ+YiT/5eIrQvixbIByPXlP8MLpXydAv+jJVgEcNL+uA7l/QnyrtVgKXlSin9QKamMjGhU8qS3b3ij0e7ubHpm/U= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(180628864354917)(20558992708506); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(6041248)(20161123555025)(20161123562025)(20161123560025)(20161123558025)(20161123564025)(6072148); SRVR:VI1PR0801MB2062; BCL:0; PCL:0; RULEID:; SRVR:VI1PR0801MB2062; X-Microsoft-Exchange-Diagnostics: 1; VI1PR0801MB2062; 4:sV8OYzJnT+YIH9h5SAYTf5Yd+CGqsZx1gC5vIRlkOtTZG0NdBpUwsUtfm43cPxlPpdg40Wtpace2T67mie5UClNlwMmgE3ihBdqjnGxOkot2eMl0Ue3O2ZPpN3L+mIXA4MJxCFrfV9SLr0ZXCEBVP8i/y1Z38Nfe5q2KI3bOoW5+7FaFPZRXSPeSgAjUcOLAUs7ekFs1JZ3QTHWGsuYqJcqan+i3oSUNfmIlOnnt8oZcMYEvV4RG0BIM7QjcpzpZXm1F07eKE2uLJVMnymNm1Xm6eQxigzSUk5kCh3jqLnt+gKWDKUPJUkbf00B89FVgsuPZAHZGbA6vUYW9HwMunFsKV3ZbLI1qiEyoaasODTMnOASVqvmBCL4rKFcIrcGIkGiM5hAZOP0Sr96KZyLCQSFEEjj4YuuUgWZwONsrjxJXxTP5tNX5//iWx6eZ7sChQurRENlNm/HSf6cXTe9wcIpE8KvoLXlHoPRqtDzetJ68G2bKErgDc6E6UI/21Z8ljk6P9uiDnTUY1VwbwWMvp8tZ3UPdTTOyLUS5TLc2XknljtoGVrMuH9JRgou+a+EsHSoo/xCfurdjF15cDBC8hpWFQw1bXG+2Op6c12+5m89EFPrFETv5kem293wxBEOtbSwOUe0fFhyKuuFgrBAaR3kVA+ZjXtyo3IMPqa5enrB5VSpzGkOVtVmz5X8FxkgQ X-Forefront-PRVS: 0255DF69B9 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6069001)(6009001)(39410400002)(39830400002)(39450400003)(54534003)(50226002)(54906002)(110136004)(50466002)(6486002)(4326008)(25786009)(6506006)(2906002)(3846002)(53936002)(575784001)(86362001)(107886003)(6116002)(1076002)(38730400002)(6512007)(8676002)(5003940100001)(47776003)(189998001)(76176999)(36756003)(50986999)(33646002)(48376002)(305945005)(7736002)(76506005)(66066001)(6666003)(53416004)(6916009)(42186005)(5660300001)(2950100002)(81166006); DIR:OUT; SFP:1102; SCL:1; SRVR:VI1PR0801MB2062; H:localhost.sw.ru; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; VI1PR0801MB2062; 23:5LRUNfEW7x/AhUUaQrrrX7SGQyuDGTTuLER+ewagW5EaifwfwnQX3Nu3iKk4GI/fdk4TzLVC613G7HUw0LTCrjh9O1k6QvDrDf41nhCcJvuiQfxHiUcGEGwn1fnU99ZjgFS0u3SIXKVe89IC4OlfhNif1NKS4N6rBmDC20mLHEzAbIjQ9jFecHRFtHzTGezWVkf+BqNaEl2cs5bcKhqFEiqUtN4NHh2J+wynIoGP9pC9vVTzr3HYVirpBIpHa4NIcfSjbesOfUY8kCGIv+vaiMjxNM8E84HzyjRw423gL3N4IO3im1tRnKnHwU0pf3LQOvwbwUCUXTA6RA0THw9MjGMa8Ws2UJnNQIupU2blVU0ateBHvxAQns0YbRsGEspV0h7oe+mgDszzlUzZ+6UNmd741Necj8SWPG956tYfs43t6QYSaho+89js4/G2zh4nTelG8JZy5xqZZ2REssYdVw7HKRg7Mj8YGfwAPx+IzlsjrpmB8lScor3PcmuGgF2LhAZPJ8OzXVWy6yTty4qd9n1vIRfxlaUigkX4ad+0V6kAEro7m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6ct/d3GuxBpgR8NAjA2r8FkWUKpBt88OzGcv7eYf8dAwOf8JmvOWo3GaBPz5WrvIOLCzKEZw/4Swzc0UDIb05hFQ==; 24:LWOxOcLCDUIxyUHFvktO/dKb9YR9+eEiRyd7PE45MSFFpR7cxxmPCrKycSUiIz2FEQyNBu8hZfuSxLKtwJTiJ2cC8cgmdTzcPrIe8MB+Zp0= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; VI1PR0801MB2062; 7:ZpJFwm/hH+7vH1YagHCy/50pp19ABB1FJp6wjHvh0SATh0isHzDBWmF2BLzIAL1rAcQBP4kSj8g0O2SrZ/ortohn1X7KEZ6I3qWBDAltwEleyADCHIvMtO3KRLKDPbxbWaDmsgfk09i2fBg9OaD1JclxQVCTR8qAkEO8t9R38wDukM/czaNMjMc7ER50aeswt7vCg8/JvfWVYIeFwpzzs/pgYCPDVj8oK3HO0rTGM7kAxvJJGGm10DVPlXxfddFeJuRcZX0x/1CEMsrfwFnCfNJMKXCBXs2h+uGPJpa3SQ2pFy+GKGYXs6Q1Su67q3EhQa+vk38aAva/OHNa8gwTgw==; 20:YPEYy0ozIt0gXEWusRAPXJjQs8kVg6JOfLOZLWw8MXgjWJouKlCnNB18IzvkKjtLp7fU1yeKPHEAT0Oy5KjjFA5dKNVI7M+Xp8EeZKHS2AGo0vU9us1oBSxFLrxUkRvPJ0lex/lhdwr5LTqYfW7ZBILqTLWw4Di0xjVyO0nCdAY= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Mar 2017 15:44:31.3586 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0801MB2062 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mark Rutland Disable kasan after the first report. There are several reasons for this: * Single bug quite often has multiple invalid memory accesses causing storm in the dmesg. * Write OOB access might corrupt metadata so the next report will print bogus alloc/free stacktraces. * Reports after the first easily could be not bugs by itself but just side effects of the first one. Given that multiple reports usually only do harm, it makes sense to disable kasan after the first one. If user wants to see all the reports, the boot-time parameter kasan_multi_shot must be used. Signed-off-by: Mark Rutland [ aryabinin: wrote changelog and doc, added missing include ] Signed-off-by: Andrey Ryabinin --- Changes since v2: - Instead of using atomic report counter, use separate flags to determine whether we're in multi-shot mode, and whether a (oneshot) report has been made. Changes since v1: - provide kasan_multi_shot boot parameter. Documentation/admin-guide/kernel-parameters.txt | 6 +++++ include/linux/kasan.h | 3 +++ lib/test_kasan.c | 10 +++++++ mm/kasan/kasan.h | 5 ---- mm/kasan/report.c | 36 +++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 5 deletions(-) -- 2.10.2 diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 2906987..f88d60e 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -1726,6 +1726,12 @@ kernel and module base offset ASLR (Address Space Layout Randomization). + kasan_multi_shot + [KNL] Enforce KASAN (Kernel Address Sanitizer) to print + report on every invalid memory access. Without this + parameter KASAN will print report only for the first + invalid access. + keepinitrd [HW,ARM] kernelcore= [KNL,X86,IA-64,PPC] diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 5734480c9..a5c7046 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -76,6 +76,9 @@ size_t ksize(const void *); static inline void kasan_unpoison_slab(const void *ptr) { ksize(ptr); } size_t kasan_metadata_size(struct kmem_cache *cache); +bool kasan_save_enable_multi_shot(void); +void kasan_restore_multi_shot(bool enabled); + #else /* CONFIG_KASAN */ static inline void kasan_unpoison_shadow(const void *address, size_t size) {} diff --git a/lib/test_kasan.c b/lib/test_kasan.c index 0b1d314..a25c976 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -20,6 +20,7 @@ #include #include #include +#include /* * Note: test functions are marked noinline so that their names appear in @@ -474,6 +475,12 @@ static noinline void __init use_after_scope_test(void) static int __init kmalloc_tests_init(void) { + /* + * Temporarily enable multi-shot mode. Otherwise, we'd only get a + * report for the first case. + */ + bool multishot = kasan_save_enable_multi_shot(); + kmalloc_oob_right(); kmalloc_oob_left(); kmalloc_node_oob_right(); @@ -499,6 +506,9 @@ static int __init kmalloc_tests_init(void) ksize_unpoisons_memory(); copy_user_test(); use_after_scope_test(); + + kasan_restore_multi_shot(multishot); + return -EAGAIN; } diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 7572917..1229298 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -96,11 +96,6 @@ static inline const void *kasan_shadow_to_mem(const void *shadow_addr) << KASAN_SHADOW_SCALE_SHIFT); } -static inline bool kasan_report_enabled(void) -{ - return !current->kasan_depth; -} - void kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip); void kasan_report_double_free(struct kmem_cache *cache, void *object, diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 718a10a..beee0e9 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -13,7 +13,9 @@ * */ +#include #include +#include #include #include #include @@ -354,6 +356,40 @@ static void kasan_report_error(struct kasan_access_info *info) kasan_end_report(&flags); } +static unsigned long kasan_flags; + +#define KASAN_BIT_REPORTED 0 +#define KASAN_BIT_MULTI_SHOT 1 + +bool kasan_save_enable_multi_shot(void) +{ + return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); +} +EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot); + +void kasan_restore_multi_shot(bool enabled) +{ + if (!enabled) + clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); +} +EXPORT_SYMBOL_GPL(kasan_restore_multi_shot); + +static int __init kasan_set_multi_shot(char *str) +{ + set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); + return 1; +} +__setup("kasan_multi_shot", kasan_set_multi_shot); + +static inline bool kasan_report_enabled(void) +{ + if (current->kasan_depth) + return false; + if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) + return true; + return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); +} + void kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip) {