From patchwork Mon Sep 11 14:57:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 13379416 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 1CD4FEE57DF for ; Mon, 11 Sep 2023 14:57:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A6BE86B02AF; Mon, 11 Sep 2023 10:57:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9F4C36B02B0; Mon, 11 Sep 2023 10:57:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 870166B02B1; Mon, 11 Sep 2023 10:57:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6BB216B02AF for ; Mon, 11 Sep 2023 10:57:25 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 38BCEC0B0A for ; Mon, 11 Sep 2023 14:57:25 +0000 (UTC) X-FDA: 81224620050.05.FBCA4AE Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf16.hostedemail.com (Postfix) with ESMTP id 652E818001E for ; Mon, 11 Sep 2023 14:57:23 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=2nXMqh0x; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 30ir_ZAYKCFM163yzC19916z.x97638FI-775Gvx5.9C1@flex--glider.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=30ir_ZAYKCFM163yzC19916z.x97638FI-775Gvx5.9C1@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694444243; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=J1LA3n6XS+9W5aKwW+ISlF8R6L/zQAaDfcEugGZs2mo=; b=hPVfoanFacwdlegIY3FpWj5IALgVB+3W46jA3cKMDE93fTi2AxrMIhLqbJ2fJv0dqoIzs1 zOxXZ/mulkkDNkyT5gvD7xytTteb+20SXWiEwla8F25OePa3xR/7IOkTQOYiRhns8OhrQC R/xh085ilOHMpLJ/90oHE2gJGXcr2rw= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=2nXMqh0x; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 30ir_ZAYKCFM163yzC19916z.x97638FI-775Gvx5.9C1@flex--glider.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=30ir_ZAYKCFM163yzC19916z.x97638FI-775Gvx5.9C1@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694444243; a=rsa-sha256; cv=none; b=yf7GoR26aaFTpdboxNfUQPm5JlmfbD1IIGx/VTSjeMb0UEsYsj6IWW1mvmsqcHAvcTsh4U mdtcBdeX7DbswKDVHrbLXAkIdrhNKvH8GrdN9Wuu7K5XcaiNjFdGvfl4a48oA+ZWJiLyHA CXeD2SBq3Q9qJwHwHXFw8smtzpciG6M= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-58fc7afa4beso53086217b3.2 for ; Mon, 11 Sep 2023 07:57:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1694444242; x=1695049042; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=J1LA3n6XS+9W5aKwW+ISlF8R6L/zQAaDfcEugGZs2mo=; b=2nXMqh0xDCCLzkPr3vNLt+TJlnjC24mUyvDx8FlHZI9nfxU/sURHeSWCM45sIpXjWB 10KKpBtIwHqt6CejE9fqsKvMs1cBLZz8QKVgPcJc/6X2fTG8ImxnKIetUL8wf/xSWwbg HGG0WuLDtVm9UM4C3dfIrPg17doFF0AaQTVQNjtx/6eG3qnoG8KenTVQzQEm9ZifEqgK fs7lZqliSLpyv6Qv1/yWR5PJahSNqvftJK6CBXDsEHXIPL+Vc+b4St41w8wRKIEBZqDX MC3U0NOZlEq4SxJcHSaP7EAMNbX3+MULPtNXGkMtwGtLgudXcWPFypmByA4QyAeEit0c lO8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694444242; x=1695049042; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=J1LA3n6XS+9W5aKwW+ISlF8R6L/zQAaDfcEugGZs2mo=; b=NlCrlRvAdHSBbt+gjIkKH3g5U5nRgV3OF1ZmUIqAwOzWRtiNQs8cGFgYwC4JFaxcqL navFj58azfsELDVY9RvxxB2x+XuL0cukPJfiCCmdPPsShT8Zz1K8hKfOTpyHZjITYyYa mENM1eLriIwIlUWBxe/0t0cdJ3kdyJQVMRrZcWHDDchxJmcA0aD6Mp/8RB5360qN0Ghg 7xCYp6aPi2CYshuG0rXb49avLP83qjxqNXXfTLjd5873Jbd/yhgyixHpY+PcImD2CAB0 hR1c4hUhfEWu0srSjIUnYH4KhT1cHU0gu1jhK4ucUEmMZSCxvOUe+R8Bgnv5oKP7Pjk6 kVbw== X-Gm-Message-State: AOJu0YyE5LfJiJA6+H58I6IvC6Y1zIeossjG+PId5ANyy8AExE+pnP7T rfWZUtoeZVTTcz++ECvmKvrilmVLx7k= X-Google-Smtp-Source: AGHT+IGgYo8cMxj1/e+FbOJOJcBQoXTfy8dYxQu7bb+1wqifAQTypCZx7AnQFhZSOBc5IXdfMrMY5NChrpE= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:62e7:6658:cb4:b858]) (user=glider job=sendgmr) by 2002:a05:6902:68e:b0:d7e:add7:4de6 with SMTP id i14-20020a056902068e00b00d7eadd74de6mr237093ybt.4.1694444242566; Mon, 11 Sep 2023 07:57:22 -0700 (PDT) Date: Mon, 11 Sep 2023 16:57:02 +0200 In-Reply-To: <20230911145702.2663753-1-glider@google.com> Mime-Version: 1.0 References: <20230911145702.2663753-1-glider@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230911145702.2663753-4-glider@google.com> Subject: [PATCH v2 4/4] kmsan: introduce test_memcpy_initialized_gap() From: Alexander Potapenko To: glider@google.com, dvyukov@google.com, elver@google.com, akpm@linux-foundation.org, linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com X-Rspamd-Queue-Id: 652E818001E X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: usb7umbh9xdo5z7o651srwm9fi4ksrbx X-HE-Tag: 1694444243-102286 X-HE-Meta: U2FsdGVkX18zSYTu6lEyryrmu42SxpQLXDCSfxry5Z8S5pKy/QewehmaIc2RFKStqbesQjuvB9GbMCOZy5iDiWHM64MMeTFxiXyTVQLsVCF9xMQGgLaB9z8TGJ2GIs7rS69OXGU1/F0YIuhUcjrNz5AF49wK8HcHIe23nqywcgic5yZEo0kf2IF07oR4tv7wCYbRNmDmTK3O8Or7Yyfl4EU5kpmcwFXCLWthpkHWtM9SWSF4GyZx6hLsElSczg8kpHCXdivxs31lnF1Zpgpk7Mu2SjiKBRx6S6sjzP8juzQIsSytAw0+ga8PEgcE5YlF6XLUiP65ZLDSO8zIIG9RsAdk8N6nDyXb8DvtvnobhaGuJh4TdLvAGIzFv2/aCSawSPsP0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0W0E8abQnUfpvHNPyIgfhJGAb3ybvCh4kQpCZ2zEJ16XpwCjm8+fQcwsOXxnPPdjm4SPH/X1nQZrPPgUKzlPn48DEJCj/HfxXuj/RA3f0LHCLYMyAFL/7mz9malC//OVvwZVuksiQBK2HFh/TRJJTX3+eq+Z4a1YksdESN4R3o5I0EbSLcbiYI9rqtjiSQWu9+kC5JYTFlhKzGX81cxBA= 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: Add a regression test for the special case where memcpy() previously failed to correctly set the origins: if upon memcpy() four aligned initialized bytes with a zero origin value ended up split between two aligned four-byte chunks, one of those chunks could've received the zero origin value even despite it contained uninitialized bytes from other writes. Signed-off-by: Alexander Potapenko Suggested-by: Marco Elver Acked-by: Marco Elver --- mm/kmsan/kmsan_test.c | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c index 6eb1e1a4d08f9..07d3a3a5a9c52 100644 --- a/mm/kmsan/kmsan_test.c +++ b/mm/kmsan/kmsan_test.c @@ -486,6 +486,58 @@ static void test_memcpy_aligned_to_unaligned(struct kunit *test) KUNIT_EXPECT_TRUE(test, report_matches(&expect)); } +/* + * Test case: ensure that origin slots do not accidentally get overwritten with + * zeroes during memcpy(). + * + * Previously, when copying memory from an aligned buffer to an unaligned one, + * if there were zero origins corresponding to zero shadow values in the source + * buffer, they could have ended up being copied to nonzero shadow values in the + * destination buffer: + * + * memcpy(0xffff888080a00000, 0xffff888080900002, 8) + * + * src (0xffff888080900002): ..xx .... xx.. + * src origins: o111 0000 o222 + * dst (0xffff888080a00000): xx.. ..xx + * dst origins: o111 0000 + * (or 0000 o222) + * + * (here . stands for an initialized byte, and x for an uninitialized one. + * + * Ensure that this does not happen anymore, and for both destination bytes + * the origin is nonzero (i.e. KMSAN reports an error). + */ +static void test_memcpy_initialized_gap(struct kunit *test) +{ + EXPECTATION_UNINIT_VALUE_FN(expect, "test_memcpy_initialized_gap"); + volatile char uninit_src[12]; + volatile char dst[8] = { 0 }; + + kunit_info( + test, + "unaligned 4-byte initialized value gets a nonzero origin after memcpy() - (2 UMR reports)\n"); + + uninit_src[0] = 42; + uninit_src[1] = 42; + uninit_src[4] = 42; + uninit_src[5] = 42; + uninit_src[6] = 42; + uninit_src[7] = 42; + uninit_src[10] = 42; + uninit_src[11] = 42; + memcpy_noinline((void *)&dst[0], (void *)&uninit_src[2], 8); + + kmsan_check_memory((void *)&dst[0], 4); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); + report_reset(); + kmsan_check_memory((void *)&dst[2], 4); + KUNIT_EXPECT_FALSE(test, report_matches(&expect)); + report_reset(); + kmsan_check_memory((void *)&dst[4], 4); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + /* Generate test cases for memset16(), memset32(), memset64(). */ #define DEFINE_TEST_MEMSETXX(size) \ static void test_memset##size(struct kunit *test) \ @@ -579,6 +631,7 @@ static struct kunit_case kmsan_test_cases[] = { KUNIT_CASE(test_init_memcpy), KUNIT_CASE(test_memcpy_aligned_to_aligned), KUNIT_CASE(test_memcpy_aligned_to_unaligned), + KUNIT_CASE(test_memcpy_initialized_gap), KUNIT_CASE(test_memset16), KUNIT_CASE(test_memset32), KUNIT_CASE(test_memset64),