From patchwork Thu May 6 21:20:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 12243557 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 723E1C433B4 for ; Thu, 6 May 2021 21:20:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DF4086121F for ; Thu, 6 May 2021 21:20:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DF4086121F Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 26A4F6B0070; Thu, 6 May 2021 17:20:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 219FC6B0071; Thu, 6 May 2021 17:20:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0BAC96B0072; Thu, 6 May 2021 17:20:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0149.hostedemail.com [216.40.44.149]) by kanga.kvack.org (Postfix) with ESMTP id E07C96B0070 for ; Thu, 6 May 2021 17:20:37 -0400 (EDT) Received: from smtpin32.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 98864181AEF21 for ; Thu, 6 May 2021 21:20:37 +0000 (UTC) X-FDA: 78112075314.32.CCEB08B Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.201]) by imf20.hostedemail.com (Postfix) with ESMTP id 063D63C7 for ; Thu, 6 May 2021 21:20:29 +0000 (UTC) Received: by mail-qk1-f201.google.com with SMTP id v1-20020a05620a1221b02902ea88445e01so1265519qkj.9 for ; Thu, 06 May 2021 14:20:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=60xM6FWdjgElSCS5XWClKw4/NbcEaCXtD8Wn+llDojY=; b=lQEStBcfVEMc1yoXYY3MarTsg1SDQgI7Ny5pMmobhwnSaRgXYyNx1S0Dl0rl5b5dAa nT/I8Vjq/ZeXNEDv/4sX+62k4g1D8fQcPtXMbuM7U3vuEgWRUQSXNypzTRCs+IBPtjxF T3ZpkUvsH2mCFZlrVXxdtqZiIGCAM4z/mBsK5UaAbmxxOTODO56eB0q1Ra9/TvYNxpJH O1rXgKtLDW6azNO8+Ae5QxedZHz5vINgjhgzqeWJyqF/TIA/jzL2nE88f8DwT2bamPIL +kcjsRFwpBUdeZ5GU310OsJvH94W6px+GZE8vZEvF/4fEMwJzKriHBcthwYyMty0PIEV 9Itw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=60xM6FWdjgElSCS5XWClKw4/NbcEaCXtD8Wn+llDojY=; b=o9C8jcBVUmN58N0Kc6oSWecqqIBDEp6MwMKMKudeTS99TYryVvPywhMLhVylW+Dx22 kvazCT6YW630yEdMIqUsqvtOb5SQWmyzVhTqgZDVc2IDn4/ouENRXYvupo+IotHnh+DR tXQGZZmKN+3WHdKvsPnoTV8JKde8KnHUTnBqF1eNEYuLZQIO+pYQzurbpuQ0ecUFW87J HPndiHbDjWHdiQc3Gh9M+aH16Xd5ycsTY6dODNaAdl7LTZWEwfOCDojdHF+2c2RIol1n tOf/7x1IuZrxHkCLW/abdKiXD4aABtSsSKNJqzhOLAop5YoG9yedIj+K/Shni3ifABGE H7Gw== X-Gm-Message-State: AOAM531q+ogXARe32UjjQ11fVCFik8TbhA+W5GqmaXTGOCeBpBlcRhTI Nz397/wcOiUp9wJavzPnK6HiJhY= X-Google-Smtp-Source: ABdhPJyK3cHsHf2SfzmG8t5qd6qZxixqZ0bc7sahr+wiq+4gc81NS5Igtz3Ug+Mc7zcQ8iPn16RKwAA= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:200:c762:3d3c:b811:8e75]) (user=pcc job=sendgmr) by 2002:ad4:4e69:: with SMTP id ec9mr6504032qvb.5.1620336036450; Thu, 06 May 2021 14:20:36 -0700 (PDT) Date: Thu, 6 May 2021 14:20:25 -0700 Message-Id: <20210506212025.815380-1-pcc@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.1.607.g51e8a6a459-goog Subject: [PATCH] kasan: fix unit tests with CONFIG_UBSAN_LOCAL_BOUNDS enabled From: Peter Collingbourne To: Andrey Konovalov , Alexander Potapenko Cc: Peter Collingbourne , George Popescu , Elena Petrova , Evgenii Stepanov , Andrew Morton , linux-mm@kvack.org, stable@vger.kernel.org X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 063D63C7 X-Stat-Signature: gbs7goo7kt5patyq7enr9cmkdawmsd9a Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=lQEStBcf; spf=pass (imf20.hostedemail.com: domain of 3pF2UYAMKCLsqddhpphmf.dpnmjovy-nnlwbdl.psh@flex--pcc.bounces.google.com designates 209.85.222.201 as permitted sender) smtp.mailfrom=3pF2UYAMKCLsqddhpphmf.dpnmjovy-nnlwbdl.psh@flex--pcc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received-SPF: none (flex--pcc.bounces.google.com>: No applicable sender policy available) receiver=imf20; identity=mailfrom; envelope-from="<3pF2UYAMKCLsqddhpphmf.dpnmjovy-nnlwbdl.psh@flex--pcc.bounces.google.com>"; helo=mail-qk1-f201.google.com; client-ip=209.85.222.201 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620336029-154233 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: These tests deliberately access these arrays out of bounds, which will cause the dynamic local bounds checks inserted by CONFIG_UBSAN_LOCAL_BOUNDS to fail and panic the kernel. To avoid this problem, access the arrays via volatile pointers, which will prevent the compiler from being able to determine the array bounds. Signed-off-by: Peter Collingbourne Cc: stable@vger.kernel.org Link: https://linux-review.googlesource.com/id/I90b1713fbfa1bf68ff895aef099ea77b98a7c3b9 Acked-by: Andrey Konovalov --- lib/test_kasan.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index dc05cfc2d12f..2a078e8e7b8e 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -654,8 +654,8 @@ static char global_array[10]; static void kasan_global_oob(struct kunit *test) { - volatile int i = 3; - char *p = &global_array[ARRAY_SIZE(global_array) + i]; + char *volatile array = global_array; + char *p = &array[ARRAY_SIZE(global_array) + 3]; /* Only generic mode instruments globals. */ KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); @@ -703,8 +703,8 @@ static void ksize_uaf(struct kunit *test) static void kasan_stack_oob(struct kunit *test) { char stack_array[10]; - volatile int i = OOB_TAG_OFF; - char *p = &stack_array[ARRAY_SIZE(stack_array) + i]; + char *volatile array = stack_array; + char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF]; KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK); @@ -715,7 +715,8 @@ static void kasan_alloca_oob_left(struct kunit *test) { volatile int i = 10; char alloca_array[i]; - char *p = alloca_array - 1; + char *volatile array = alloca_array; + char *p = array - 1; /* Only generic mode instruments dynamic allocas. */ KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); @@ -728,7 +729,8 @@ static void kasan_alloca_oob_right(struct kunit *test) { volatile int i = 10; char alloca_array[i]; - char *p = alloca_array + i; + char *volatile array = alloca_array; + char *p = array + i; /* Only generic mode instruments dynamic allocas. */ KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);