Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp267065rwb; Mon, 26 Sep 2022 12:04:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4BqMw/AhFJ3cvwjlfqN+XXXVIUjAS1hiQ0+jwqe5jbcTT73k5FeqWSZBkUm6qoiPf3mH+H X-Received: by 2002:a65:6d93:0:b0:42c:50ec:8025 with SMTP id bc19-20020a656d93000000b0042c50ec8025mr20529219pgb.62.1664219060668; Mon, 26 Sep 2022 12:04:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664219060; cv=none; d=google.com; s=arc-20160816; b=FWkyC3/dDLJ/8qMi1XUHgqo0kO4v6tzLeXOw8RmogrR2LbaTpGWOrzeOjMzQl/Hx6S 3U0hV6E1MzargEC2NDfDi2tpafjtKQXhsa/RIMMNTebQYrKXYtqTvfXfU6U+2chJpJ10 SQPO7dPGzC/Bvi5aqPyOEa0RTTTQoQYWVUjYpb2ctLlZiwE3pkQv0v890juHvb3uVl+2 FJ+TSptBngKbvcCTXr9QxA+AL35390lDBcwmD/pznKt+VHscojRkkEB8VYQa+WTKw+79 KDxLkOJHviUPvGXyC00EZKty4cC/vlqb8KEpqVEgSRhZ1Cbl3z1zHc37C0BX+6g39IsP HeLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=OEmgNjB4wCnJJ/8lBPr8SNJcQWguKZ9xXHXJI9R5iDU=; b=iGcrxLFuFMJYxkEsxg9BDa0KJqldGslplVVZtoKHiKGv69DeL5WJeluAH8ldLOxiVD 4gR6D5EX+qSJqn+Hz5wa8AOzdDra1RhoxdL6ozXO/b5bYYwQC2iFqxD2Mt5uWwEQ3+ID XmE9btZnLugMzs3EehVZUaCzOqgGH+qx1lVYjSaV2871aXIjcMTfLzKioCaNW3Jp5hGK nUHvrSRtFdtBlRRt1BIhbbs6q0iPFiicTnbeCXZes1BUdrPWUDVGiCUlKaW/iWTZ6NdW x3v/q8D5dOmT+zYt3eaxEYP7c6UYjOU02XJYzT9mePX34op1Dz4RutKrQI+ZwiPiqeNN dKAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=BRXYv2H5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j13-20020a170903024d00b001749dafb822si14204206plh.507.2022.09.26.12.04.09; Mon, 26 Sep 2022 12:04:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=BRXYv2H5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231396AbiIZSSZ (ORCPT + 99 others); Mon, 26 Sep 2022 14:18:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231334AbiIZSRz (ORCPT ); Mon, 26 Sep 2022 14:17:55 -0400 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F7F36307 for ; Mon, 26 Sep 2022 11:08:53 -0700 (PDT) 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 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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); -- 2.25.1