Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2040765imm; Thu, 20 Sep 2018 06:57:12 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaMmq9yH8uAiut1AI9snOYaVRRxNhTBf1DIezoqwFdiTfERCR81WRt2L6qNkbhwitAoa6N9 X-Received: by 2002:a17:902:4401:: with SMTP id k1-v6mr38451298pld.97.1537451832414; Thu, 20 Sep 2018 06:57:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537451832; cv=none; d=google.com; s=arc-20160816; b=W8S2lHE2ofEcm0vBuFYK6rb7HEMHdRiTCrmxTn2+SfZHFt1XhZSxtSN2p5smk6FCpb ZwFrLHcFmYPs9ffTnPx4FUrcDKmGcrMB1QRvrC5Sc8LkY1eAu4GbSWKi8GBsHjkWYAmV 6s8+DGFCnPmKV2A3ml8aq2Gn02U3liY6aYNKYjlLb0JBxJiysUdCnHw29ffkQiUXdIxz v7rzGomJTFchR3zCqCFANJCanhrlUj6dkFR4S9h5j+6uM1VNnicmzucmRoIegrzBlnly A9bc00WP5kIDbHtiDZiLz/SU4Q2ZvLXsKWu5rIm45Dl9H//49eTQrNj5Q1CrttmzJty1 xVfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:spamdiagnosticmetadata :spamdiagnosticoutput:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=XlYm+3iE/PMapwymuU7ypiGUKr2ts7a8XIdUgdN5gzs=; b=S8p3xpsf8CWSF6qanPElNeaDc+pIXbQgNtaYZv+EqwTwUfpXjfm5G6Q6vYAgzNlVCx YhzKXMBAbIfvwjWK+WN3ayW0vsyC0V4IRkr6yZgcBxgFU8an+Umo4q5eFb/36rYoA859 iqPPxeOj01orrGJq6nsSsQ2ffAk50qFDkF3RnIfN17zPoJZP0XmEpcB/m8RPlVS3mX5F GgtmT7UzpAO6Ht3H/HrWzyePQbCsOaf67HNYkn3LPzRht4QDv4PgUjynhmZvcO38OTw0 lDEBsPbQQ1aVLzjI3gpKiyaFTdUoiPKQsn9bOAxtBBHlK81JbpBjuF1WSmSGWx0pX42B clTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=selector1 header.b=YJ1BaxZI; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p126-v6si25183174pfb.77.2018.09.20.06.56.45; Thu, 20 Sep 2018 06:57:12 -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 header.s=selector1 header.b=YJ1BaxZI; 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 S1732912AbeITTkG (ORCPT + 99 others); Thu, 20 Sep 2018 15:40:06 -0400 Received: from mail-ve1eur01on0131.outbound.protection.outlook.com ([104.47.1.131]:6684 "EHLO EUR01-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1731223AbeITTkF (ORCPT ); Thu, 20 Sep 2018 15:40:05 -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:X-MS-Exchange-SenderADCheck; bh=XlYm+3iE/PMapwymuU7ypiGUKr2ts7a8XIdUgdN5gzs=; b=YJ1BaxZIKZmQuRUoD8O50+wi1ccFRIUg49Yz5HPE6xoySgz19OPyK8CF9zezdCV4enY5ilt3qa/KlyCuyLbFcJ6+lq8zzCN7WGf8FmMlhHINHMggpxSlHMmAGLtEjW9v4G1KFMuBF3pq5l7yi2EEmUCOTA0qSGAvbNu/b9/OyoQ= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=aryabinin@virtuozzo.com; Received: from i7.sw.ru (185.231.240.5) by VI1PR08MB3264.eurprd08.prod.outlook.com (2603:10a6:803:3d::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1143.17; Thu, 20 Sep 2018 13:56:22 +0000 From: Andrey Ryabinin To: Will Deacon , Catalin Marinas Cc: Andrew Morton , Kyeongdon Kim , Ard Biesheuvel , Alexander Potapenko , Dmitry Vyukov , kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Mark Rutland , Andrey Ryabinin Subject: [PATCH v2 3/3] lib/test_kasan: Add tests for several string/memory API functions Date: Thu, 20 Sep 2018 16:56:31 +0300 Message-Id: <20180920135631.23833-3-aryabinin@virtuozzo.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20180920135631.23833-1-aryabinin@virtuozzo.com> References: <20180914152800.GB6236@arm.com> <20180920135631.23833-1-aryabinin@virtuozzo.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [185.231.240.5] X-ClientProxiedBy: HE1PR0301CA0004.eurprd03.prod.outlook.com (2603:10a6:3:76::14) To VI1PR08MB3264.eurprd08.prod.outlook.com (2603:10a6:803:3d::19) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: fda58248-52c8-4843-dffa-08d61f00d9e2 X-Microsoft-Antispam: BCL:0;PCL:0;RULEID:(7020095)(4652040)(8989299)(4534165)(4627221)(201703031133081)(201702281549075)(8990200)(5600074)(711020)(2017052603328)(7153060)(7193020);SRVR:VI1PR08MB3264; X-Microsoft-Exchange-Diagnostics: 1;VI1PR08MB3264;3:F4gHzjgawxaNH2SFRlcVgA+WHmdddJ+YVAAWeHecQEDoOhLB5EMVrtFRp8eCpOnIIbZgrA3c0RmfNCJhm1oJtBShvNi08yKgi60BPNee3iY5TXyYh3rVkL7F5LCK99v16nKW8eJ4qUBqVRkd3X217IIrez6vX1jKSZVePK4kHkbLM9VwDSg5feE4Z5YaJbdDWrniPDdL1CeLjIYHjICurcIfIm0yHV2wX8cktobhzIiBy1iJ2zghePbDkMUvS6z1;25:RhgJ7SZtBIvqKKCBMXszSOQswjh4twYyJZMm7OaUuxDSGcidOi/b+MJXPUwKsiwn6LhoGl/rO/1+3snSAyVZQz2HkujLUevUhgOESEZs35ZqDpPUUkpGKPTKFF9K63BNhN4fnkwZpFYiwMeJaYcPMoMzrou3MEGqcrIk0XpXwDi+uD1jfKZwVQWAzlQw0344WHikoXezo25MWgpFYY6Ir7YQl08RCf+VYq8mliG9aoh/mHt4mzgO67OSFF8i9hAsGq85sn1QFpVlbP9B3G409b7H3C6MEOou1WdCkyqugEE/Gx7T3wM0vh61REfE13dlt3QmVwvNSWcvXVpFsVHbjA==;31:KQL5KSq31tQV7jbIA3+Qqty1SntPBnbM/RyOXsBg/GT8HIvKmRCwa9Ze5y6Wdj4xf9UCYr2I/fuLpr3z2bgkSrDaN3NFYFUqmqOG2MSaVoIDIix6fGlKKVcxsscFJXjv4r+HUyDqLpTwFtxunIRoRQP7GkSeN+CmkB0hLMdz4U6aerIHTjb0WnFeuNHZtVdXSngRkUgm1wsOUiu41Cg3v5HWL6AJgl3H04mQUvvUywM= X-MS-TrafficTypeDiagnostic: VI1PR08MB3264: X-Microsoft-Exchange-Diagnostics: 1;VI1PR08MB3264;20:UOgj79MHU6/KQdPMPtrKKlwHRqgD7qIlQjIK/0468XQm16QOoi67zQsyCZS59qiBmlxx+I/Ocmu6sUyZDRRLqTucQ7/mYWVcM/LoD1TO8QIN4dTP2yHC53fBlGCjnDH27M+k5gkkeBQ5azadciQhtoVKVyU5+d87HFZi/5R1ErGl3hLDtYLqHnnz9usXVP6A9DboVR3XsMCL4JP/Gidq0orlONUAIWvnpRht3xOAcmhAH3hMf5ReS9wsvlxlhHXBBZeEOStM8NVBmVYDVQX8wHAAc5+VcSaDIAojN3F2bKKWrnJGo8Z+Ox39qvGd0faB+3gfVeQ6MkIRXhIyZhbmJ9IioYo7SbA8vzGv+y44oR1aRgEKqGlrzDs+DXo9nYP/FMcy5sWJRoKYRZEIzQ0ohkitslm+bN4OPCnZi8pHd5cmo/97fHn6t2w3zv7qAUoLOKOZ8rIQyWkCEgchW6ke1TTDzcZ6kvAda04HRm7sX0k5deK2+h0WMbF6TvHZ4733;4:lEr3P9Ms5iA5e2vUVoU6GXi7/3ddmptwUNlACjB0qmvg9Dug8nBkfagceds09UxMDCxf6OblTlj4oiMH43lg28E1kDYAj2sWZw495S/pCQtnM5BeMES8AoAIj2ITlySLuFRs1AL9uLAnOcKoecChW3d5l0MbgGmUb/56xWl7yqmRGHd24WWBWxAiHteY3jJImXb4zHIP+gljTrURkwf6hQgGMYhWN0hnUUIs0kp7TaMdUukUsTzh8oMIXZ3JLEYMOlUAC+64+Rhol9QHJS9Shg== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-MS-Exchange-SenderADCheck: 1 X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040522)(2401047)(8121501046)(5005006)(93006095)(93001095)(3231355)(944501410)(52105095)(3002001)(10201501046)(149027)(150027)(6041310)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(20161123564045)(20161123560045)(201708071742011)(7699051);SRVR:VI1PR08MB3264;BCL:0;PCL:0;RULEID:;SRVR:VI1PR08MB3264; X-Forefront-PRVS: 0801F2E62B X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(979002)(39850400004)(396003)(136003)(376002)(366004)(346002)(189003)(199004)(68736007)(52116002)(7416002)(7736002)(956004)(6512007)(11346002)(50226002)(446003)(386003)(6506007)(1076002)(6116002)(8936002)(476003)(50466002)(16526019)(486006)(97736004)(478600001)(186003)(8676002)(53936002)(48376002)(86362001)(2616005)(26005)(4326008)(107886003)(81156014)(106356001)(2906002)(105586002)(305945005)(5660300001)(6486002)(316002)(6666003)(51416003)(53416004)(16586007)(110136005)(81166006)(76176011)(36756003)(66066001)(14444005)(3846002)(25786009)(54906003)(47776003)(969003)(989001)(999001)(1009001)(1019001);DIR:OUT;SFP:1102;SCL:1;SRVR:VI1PR08MB3264;H:i7.sw.ru;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;VI1PR08MB3264;23:fwLF2pJ15qu2p0J2JveRrOBWgZI2qW+iFTRbBr48l?= =?us-ascii?Q?JI8zaLdXbE45fMHWq9TlmylZtQD7e3IdeQW1ev5LawN2x2Imi63XXOYsQA8I?= =?us-ascii?Q?YaEzxVUmmIgKeKFCnr8pQuvU3C15FRpwIawqkG/b6aY0zFnfj3RwiugOrNDS?= =?us-ascii?Q?e4WnV7zcj10woI0d+/6ngrvBqzPZnBpqdsVzOTlzAFmQzX1LArTV2YWG4FX1?= =?us-ascii?Q?2VkSfqvSMjbrYzJ51fuXdwvr9yNaM/zBYJaEtzXy2O7XGInEm0IQsSZHDIp0?= =?us-ascii?Q?VH5wdQMA0JjOUIGnbWlUB2m6PxhzPhW6uSxQtJmGwOhnkLjgzxFw5vA/yfy5?= =?us-ascii?Q?WyOIf5tTuiqUeSbLxj+KBxWrlfHx5ClTIu2zD8Nhz9kUK665WTGcuPx7PCAs?= =?us-ascii?Q?tSodZAmVVTG3vE8A0D8kvtGJxKgG3vB54CU7W4niB5SExHfKOKgEhKlVXa0N?= =?us-ascii?Q?mb/KEw9WY4IqJUo//C0MweMbf4xLnbXx5vW7Or1ta5wfjzq2F+BQf4jc25+k?= =?us-ascii?Q?TcnN4hG/QltL3UCliXKXK08csZPYR1/s0tBKAsNa3042XSltgTOvWEwUIJqA?= =?us-ascii?Q?9V0OvpqWsCguVrQQzPky1K7KPv/tgXEPFvqkQ6qXbWrUs6CHqVJMoYlapb6t?= =?us-ascii?Q?fp/LBjyy8tX5K2vxR5IPraK9YYxGRuT11NwTxN8cMgWvyxb37b/62jLVeu8j?= =?us-ascii?Q?lKq6pHUTKs4Wyq/utrH/nEvSiNJYh+ZKDp5Pn5XKCSOHPv5oXN/gqBEWExdf?= =?us-ascii?Q?WlgjP4/XqpB1lwVCQWgIPMxxV7FgQUh88BXq5U09NBylayUrnghbxcSerzXv?= =?us-ascii?Q?SsI0XMhsOcS+YIUvCT2GtZ+jMDgCP+TrgEIyGIN9zpMClOu1N6/pYq6sexJi?= =?us-ascii?Q?P5/VOFycqpTSbDxX86oaHwno8pYcDwotrQqderOl78EmIBKTOHDnK3clJP7f?= =?us-ascii?Q?s0xGDzul/BgKMNJMIXukw/kZU4wSGmSO8UKYwUTn/5q1IvcydyBI70tqj9cJ?= =?us-ascii?Q?IDaKCiqx4ms0bynndENLnmZEphIWlcUppEHQK694u3nUf61QsObV9smbzN9r?= =?us-ascii?Q?codbKSzVbz2G3zh8Cxjq9fN+A67MSNU8Zd7dAG0m2iJx3WwbCENVabUindss?= =?us-ascii?Q?H6WykhkWwRnq4lHHplQ52PlLk2j4ZD7ELjs3T3jN6l2imRqDo1RVUMCsbOka?= =?us-ascii?Q?tz5Qs/51kTBpIZUtX3efQOXAFsQpLqFKvDC58hjKZN/9OJjamN4bN5+EbQ0f?= =?us-ascii?Q?4g/dBuKmMNbG78TFd8cwNl7eSFqmn9cINjGBzjEiV/B7xJfXj1Lw0ynA9nGs?= =?us-ascii?Q?ED0gyuEweqx/bYljlCuGU4hGF0AcyCM7CcxNvglDUqrWdSsv1ViWKFAwh1zS?= =?us-ascii?Q?KqtEcjK/4rWSioofl+e5UPqTFA=3D?=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oI8OjqtZ7S6Pyd0OejVmbaQF0W0NC6SOdXYHdAhjyJbqpPYZuDYZMtQTfiAUL7ZFzuKYynQ8Hb8vTIuxm8GmXWsBvMUxtI2T0lbOErZePGaf1mfM0mX2Hc7qIYauqFeB34eCY+wiQsn14Jh7TGIFuzPhWAkMMTiI37qBUjeI3BpbN26WAEoG7x1J7/2Fwl9Q07GWHzRfgTHGj23ZY3cApf91ZMV1RXhnBUkO9pAat1ty8566qGW7 SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;VI1PR08MB3264;20:SeR0VJrF/KfUSNYERUmvwC4089kUh9EwUxLYUw/hGS+7Vbptu/ejFXa1/vW7iggUqAipXg3xyT/xBsdDNXZ9Hvqe7RV+MeG8qh41AA4fDsQTuPN+sqcGzoES5Y6Tuz57Q7Jptysffifmr1PszxTOMkIxQzFV4cHG83s4UaX8J8g= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Sep 2018 13:56:22.2871 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fda58248-52c8-4843-dffa-08d61f00d9e2 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR08MB3264 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Arch code may have asm implementation of string/memory API functions instead of using generic one from lib/string.c. KASAN don't see memory accesses in asm code, thus can miss many bugs. E.g. on ARM64 KASAN don't see bugs in memchr(), memcmp(), str[r]chr(), str[n]cmp(), str[n]len(). Add tests for these functions to be sure that we notice the problem on other architectures. Signed-off-by: Andrey Ryabinin --- No changes since v1. lib/test_kasan.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index ec657105edbf..51b78405bf24 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -579,6 +579,73 @@ static noinline void __init kmem_cache_invalid_free(void) kmem_cache_destroy(cache); } +static noinline void __init kasan_memchr(void) +{ + char *ptr; + size_t size = 24; + + pr_info("out-of-bounds in memchr\n"); + ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO); + if (!ptr) + return; + + memchr(ptr, '1', size + 1); + kfree(ptr); +} + +static noinline void __init kasan_memcmp(void) +{ + char *ptr; + size_t size = 24; + int arr[9]; + + pr_info("out-of-bounds in memcmp\n"); + ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO); + if (!ptr) + return; + + memset(arr, 0, sizeof(arr)); + memcmp(ptr, arr, size+1); + kfree(ptr); +} + +static noinline void __init kasan_strings(void) +{ + char *ptr; + size_t size = 24; + + pr_info("use-after-free in strchr\n"); + ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO); + if (!ptr) + return; + + kfree(ptr); + + /* + * Try to cause only 1 invalid access (less spam in dmesg). + * For that we need ptr to point to zeroed byte. + * Skip metadata that could be stored in freed object so ptr + * will likely point to zeroed byte. + */ + ptr += 16; + strchr(ptr, '1'); + + pr_info("use-after-free in strrchr\n"); + strrchr(ptr, '1'); + + pr_info("use-after-free in strcmp\n"); + strcmp(ptr, "2"); + + pr_info("use-after-free in strncmp\n"); + strncmp(ptr, "2", 1); + + pr_info("use-after-free in strlen\n"); + strlen(ptr); + + pr_info("use-after-free in strnlen\n"); + strnlen(ptr, 1); +} + static int __init kmalloc_tests_init(void) { /* @@ -618,6 +685,9 @@ static int __init kmalloc_tests_init(void) use_after_scope_test(); kmem_cache_double_free(); kmem_cache_invalid_free(); + kasan_memchr(); + kasan_memcmp(); + kasan_strings(); kasan_restore_multi_shot(multishot); -- 2.16.4