From patchwork Mon Sep 26 18:08:47 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: 12989224 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 E591CC32771 for ; Mon, 26 Sep 2022 18:08:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5FB838E0078; Mon, 26 Sep 2022 14:08:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A93F8E0066; Mon, 26 Sep 2022 14:08:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 499408E0078; Mon, 26 Sep 2022 14:08:55 -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 3B09D8E0066 for ; Mon, 26 Sep 2022 14:08:55 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 07E6214044E for ; Mon, 26 Sep 2022 18:08:55 +0000 (UTC) X-FDA: 79955022630.20.5551F43 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf06.hostedemail.com (Postfix) with ESMTP id 1A932180017 for ; Mon, 26 Sep 2022 18:08:53 +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=1664215732; 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=OEmgNjB4wCnJJ/8lBPr8SNJcQWguKZ9xXHXJI9R5iDU=; b=BRXYv2H584+i+PNOK70i9qNvTUWg/p+2sFGdXri1p9nBLJi3RZSOtMmUlDiFOKaO22xIGa JLzhq+Hhl4BGW8Yct87UHnN4sLdRqozlm9FYN160o6gDw7B2NOBfZ9d3OQO6xkAwjmGyqb 3VcGdLmgtTGBoN82/zZAh66KS3AAR4g= 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 v3] kasan: fix array-bounds warnings in tests Date: Mon, 26 Sep 2022 20:08:47 +0200 Message-Id: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=BRXYv2H5; spf=pass (imf06.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 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=1664215734; a=rsa-sha256; cv=none; b=78Jixf3wQLGiobM74Jq5vvLR0vlHmcCZlY1UIJ8KoeIKDYG9AytJwB6Y5VOGciChsUqnLm Ojz8aP/u0MBEwMQV1YDIjiBmSyC1xU0/j7mj00ma4riOK87LnYu5sgh4ib/SqfI5/dpG1a Oq+VXbduOxhrmBoJH11MDEfvHlUmJ2Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1664215734; 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=OEmgNjB4wCnJJ/8lBPr8SNJcQWguKZ9xXHXJI9R5iDU=; b=YKrEoH0IU3bHE/iI+Qd4MPpXmgdmOh2gLr/RkKoNNS+VXtRr/7lFJpgaI0s+PihoYV2SnA YTsou85gKik8V0/Fyg0gnhJndo9OLS54SViCqgqR4ydpEhD7le3i1R8oN4hh3H76q22AOb qlCPQEsZVcVGfnowzmpIFFUi1vxdG2k= X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 1A932180017 X-Rspam-User: Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=BRXYv2H5; spf=pass (imf06.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Stat-Signature: yi38ngf984norpmgdm9j6rst1eoe3hn1 X-HE-Tag: 1664215733-168187 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 Reviewed-by: Kees Cook --- Changes v2->v3: - Add comment for added -Warray-bounds-related OPTIMIZER_HIDE_VAR. 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 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c index 71cb402c404f..f50b11d84f41 100644 --- a/mm/kasan/kasan_test.c +++ b/mm/kasan/kasan_test.c @@ -333,6 +333,9 @@ static void krealloc_more_oob_helper(struct kunit *test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); + /* Suppress -Warray-bounds warnings. */ + OPTIMIZER_HIDE_VAR(ptr2); + /* All offsets up to size2 must be accessible. */ ptr2[size1 - 1] = 'x'; ptr2[size1] = 'x'; @@ -365,6 +368,9 @@ static void krealloc_less_oob_helper(struct kunit *test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); + /* Suppress -Warray-bounds warnings. */ + OPTIMIZER_HIDE_VAR(ptr2); + /* Must be accessible for all modes. */ ptr2[size2 - 1] = 'x'; @@ -578,13 +584,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);