From patchwork Sat Sep 24 20:23:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12987824 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70DC2C32771 for ; Sat, 24 Sep 2022 20:23:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E27688E0015; Sat, 24 Sep 2022 16:23:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DAF648E0007; Sat, 24 Sep 2022 16:23:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C28EA8E0015; Sat, 24 Sep 2022 16:23:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id AD85D8E0007 for ; Sat, 24 Sep 2022 16:23:26 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6E53780751 for ; Sat, 24 Sep 2022 20:23:26 +0000 (UTC) X-FDA: 79948104012.23.A9AF6BC Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf12.hostedemail.com (Postfix) with ESMTP id 01F4E40003 for ; Sat, 24 Sep 2022 20:23:25 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1664051004; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=nC3nuVqN+jtEUN74Nl68oDoRxZZaXpDu+Kubg+3Y4uE=; b=K5b/cTkQVPZHrmVwC5a5EZ6fbHafwPdgOTRisgbxnFrlDyFsNV8EppN8s8pwk3WvoYaKMh 2jMjRVOcQmLC7gpvopmtr3Sjin8ubOEGUnRny9AGIWZGq0fj7LCCb784kCWrUGCCFlpiDQ mFqrQzCTdUdXXfuIweR95PiUh6ngdA4= From: andrey.konovalov@linux.dev To: Marco Elver , Andrew Morton Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, linux-mm@kvack.org, Kees Cook , linux-kernel@vger.kernel.org, Andrey Konovalov , kernel test robot Subject: [PATCH mm v2] kasan: fix array-bounds warnings in tests Date: Sat, 24 Sep 2022 22:23:21 +0200 Message-Id: <9c0210393a8da6fb6887a111a986eb50dfc1b895.1664050880.git.andreyknvl@google.com> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="K5b/cTkQ"; spf=pass (imf12.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1664051006; a=rsa-sha256; cv=none; b=uYK00QwH2VX0lnI2DsQTEVuuSU7N3CB5+WCILdjCI4neSfYrDM3uoIAt/WvHDV/KujCk/o lJz7zQtCBiAHo8UOwExL6c8n2GBoqzzOVjYIzaAlOZke/Pr8vQyekcI+83BPBeVCqEQZ+y e9KV/cG9+eJrnixRjq+osiq/OWpAO6M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1664051006; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=nC3nuVqN+jtEUN74Nl68oDoRxZZaXpDu+Kubg+3Y4uE=; b=mR7B1Q8xe2UJHHzQoW05OEJirl9m3/GkmimCME9y7aYVw18dOGXalFve9m4IjJeOTbC3i4 sbgupJ30KSUBzXbAI6iBvJbGGSwE3z25b+W1VZoFPV+s39FFqMzVZ2eic3txrOvmgZPjrg a+qQJctwbZP9Pv6/Yr/UHjNfPXcQqS0= Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="K5b/cTkQ"; spf=pass (imf12.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Stat-Signature: 7esbrsgetod7rmmrupf1eed7nupgza9u X-Rspamd-Queue-Id: 01F4E40003 X-Rspamd-Server: rspam12 X-Rspam-User: X-HE-Tag: 1664051005-338759 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: From: Andrey Konovalov GCC's -Warray-bounds option detects out-of-bounds accesses to statically-sized allocations in krealloc out-of-bounds tests. Use OPTIMIZER_HIDE_VAR to suppress the warning. Also change kmalloc_memmove_invalid_size to use OPTIMIZER_HIDE_VAR instead of a volatile variable. Reported-by: kernel test robot Signed-off-by: Andrey Konovalov --- Changes v1->v2: - Hide ptr2 instead of size1 and size2 to be consistent with other uses of OPTIMIZER_HIDE_VAR in KASAN tests. --- mm/kasan/kasan_test.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c index 71cb402c404f..dbb0a672380f 100644 --- a/mm/kasan/kasan_test.c +++ b/mm/kasan/kasan_test.c @@ -333,6 +333,8 @@ static void krealloc_more_oob_helper(struct kunit *test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); + OPTIMIZER_HIDE_VAR(ptr2); + /* All offsets up to size2 must be accessible. */ ptr2[size1 - 1] = 'x'; ptr2[size1] = 'x'; @@ -365,6 +367,8 @@ static void krealloc_less_oob_helper(struct kunit *test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); + OPTIMIZER_HIDE_VAR(ptr2); + /* Must be accessible for all modes. */ ptr2[size2 - 1] = 'x'; @@ -578,13 +582,14 @@ static void kmalloc_memmove_invalid_size(struct kunit *test) { char *ptr; size_t size = 64; - volatile size_t invalid_size = size; + size_t invalid_size = size; ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); memset((char *)ptr, 0, 64); OPTIMIZER_HIDE_VAR(ptr); + OPTIMIZER_HIDE_VAR(invalid_size); KUNIT_EXPECT_KASAN_FAIL(test, memmove((char *)ptr, (char *)ptr + 4, invalid_size)); kfree(ptr);