From patchwork Thu Oct 10 13:11:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sabyrzhan Tasbolatov X-Patchwork-Id: 13830100 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 5CF3BCF11E9 for ; Thu, 10 Oct 2024 13:10:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B2C906B0085; Thu, 10 Oct 2024 09:10:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ADCEF6B0088; Thu, 10 Oct 2024 09:10:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 97CDD6B0089; Thu, 10 Oct 2024 09:10:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 7B0DC6B0085 for ; Thu, 10 Oct 2024 09:10:38 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7B037C02D8 for ; Thu, 10 Oct 2024 13:10:34 +0000 (UTC) X-FDA: 82657726956.23.E98A0FA Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) by imf16.hostedemail.com (Postfix) with ESMTP id 1688F180014 for ; Thu, 10 Oct 2024 13:10:34 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=N9J+32RH; spf=pass (imf16.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.167.44 as permitted sender) smtp.mailfrom=snovitoll@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728565792; 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:in-reply-to:references:references:dkim-signature; bh=OuJiu2A9K3F7RxlNpRBbTBdPuLTp339ni2HcpWnX9u0=; b=LHTiDfbtb2ZVMbIiwen9HrmulqN0ugLiqv6uAysav/XQ6y98i0jgY78C6c7KiTt9WN9Se2 0o4S6DifwViDUYaNzzWcrQs6DsA6y2CgxWYEecr2T7ULnbTGxhT0uaZiq44uPp/Yz7o5nA FI2GxjRM1OFQFF34YygxVuwK3UBPIeg= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=N9J+32RH; spf=pass (imf16.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.167.44 as permitted sender) smtp.mailfrom=snovitoll@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728565792; a=rsa-sha256; cv=none; b=d/OyLdFTg1Bh93lUDbORQqwBCfhQesYNd8lOH+hPqMGpNgxRNVEJ5Hr/7btB6ljVvAmSmi iJ/9ZMuGJHXAK0Dmdjvsg2FiTUWJFlVKbUDIQtAvMsHUYB2y39KBYaG06jh0eGRsbPnx6l NCpxyvtFOgaTLg1Z/zSxY3gK3RwK/J4= Received: by mail-lf1-f44.google.com with SMTP id 2adb3069b0e04-5398e58ceebso846645e87.0 for ; Thu, 10 Oct 2024 06:10:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728565834; x=1729170634; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OuJiu2A9K3F7RxlNpRBbTBdPuLTp339ni2HcpWnX9u0=; b=N9J+32RHnih9cwoylIbX38NcvKMIyQQkhZA+CUjk963rLXqVtxsEiK1cY1lpAZmGZt D9LHqpWOoTUQIH4lnfRNwI0jWerEHlml089dm4FWlSSGOh8+2DwLyfiXC3qqrvrk6MbR T8Ts+vsv7lnJGPV0yyZk83JHEN8r3C+KTAfU+MNKscgNRBGDoxUXWQVGK+7vnxgn69T3 mYEu8PStwGFFiHcQ/BDR10drjuteHnWnyy05v+YoClb/Q9Ni0P9TacHUU48LD/G34jeH HOF68fr9aPaKgrnV3eBKCFhc3WV1S6VjGg9RQtV7oU9ZdJE0ZBkXYDk4bpNUiLClt2b1 qxPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728565834; x=1729170634; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OuJiu2A9K3F7RxlNpRBbTBdPuLTp339ni2HcpWnX9u0=; b=eYl5/8j2Jz9j/As+PmBhKDXsdlenkbTBpFu64zEVUvs+bHRrA4Mrd9e+wMEZQ/2DPO EfZsfLHa1agbgKWWOjrbYCG8vrHQqtJtSP8as+BP0vBbVJWyn5iX8gq+xk1X4PFyiDhk VuNNlBFZ5ZrG4eLfvqBt7mg+MoFHHo7kDOgaR5XcyH4GFfh2FAb4+pXtT2xNFwPU/GBw cOjj7Zfv+DoJIUkPYugm/IKS5P6WvBmnB9YT+DYt+cLlBvgVUL3m/53HxDd6pcZW3Fti A6QqaypW8oNEEF+zWeOMRJnQutQeqPjOpULLi3b5XMjCIPF9rDFWHXe07BQYHswAVAOU 2YmA== X-Forwarded-Encrypted: i=1; AJvYcCXMqzRCYtFnRub9WY588qSkFfLb9gT8HXRiowszL1VZvEHNmIlH2qtleG0cxo9G2kGGzGa4co5JQA==@kvack.org X-Gm-Message-State: AOJu0YxNabTXlFiUXoa5RyPCBcHy0VeU8odsqKlNtzS8Y3lVdru41tq1 XSa+vAPXYab7O3ipk/dk1S5uXxy3S3Yf+kuCHPr6ZiavPxbJvPaVYprZwwPf26M= X-Google-Smtp-Source: AGHT+IHotmSx4lEVD7Vq09kHOuUrD8kLvlKE58bqQ/6ks9ovPPFH5W+V24e63oyTtG9WOJ6NWDRz/w== X-Received: by 2002:a05:6512:3d10:b0:539:933c:51c6 with SMTP id 2adb3069b0e04-539c9895961mr1182236e87.29.1728565833803; Thu, 10 Oct 2024 06:10:33 -0700 (PDT) Received: from work.. (2.133.25.254.dynamic.telecom.kz. [2.133.25.254]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-539cb8d800esm248596e87.126.2024.10.10.06.10.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2024 06:10:33 -0700 (PDT) From: Sabyrzhan Tasbolatov To: elver@google.com Cc: akpm@linux-foundation.org, andreyknvl@gmail.com, bpf@vger.kernel.org, dvyukov@google.com, glider@google.com, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, ryabinin.a.a@gmail.com, snovitoll@gmail.com, syzbot+61123a5daeb9f7454599@syzkaller.appspotmail.com, vincenzo.frascino@arm.com Subject: [PATCH v5] mm, kasan, kmsan: copy_from/to_kernel_nofault Date: Thu, 10 Oct 2024 18:11:30 +0500 Message-Id: <20241010131130.2903601-1-snovitoll@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 4m4w55kxtd1djbxmhth498r1ubkf8yfj X-Rspamd-Queue-Id: 1688F180014 X-Rspamd-Server: rspam11 X-HE-Tag: 1728565834-304689 X-HE-Meta: U2FsdGVkX1/d1Q0+Pjas+FkyR+/KHmK31PM5Org8RlCyLriGST+Emzn0/TZMwinsNlsPBYwAqjmE4X7UBjud25QEzIUJrnFd5DWZ+8wVGATTEhdwBIefXVQZbU0EGrTJqUA9I+yMj4y5n1MSidtF3byiq3wffJVACsDEZL+WDPZAYHjM/TopuEPmWla7MYvwfjP7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esiowYLSDJt/RpkPVB70osfmlb+OGnodDJwBQwD5WGJv346CyouY9SxFHkCTYEcwCrQStQNs7IEHPhXO9z3h8GfPua7+gahIB9VOJrPiY8iCF9iPR6vSB8YNPMVXWjXcmbumdMttJ9brawodjwXk6d0O/plX4/v42Y5XUBRnF68a+5auz8FQj8frmwzwvi6zCuCKgC1UXcli8MXL284rg2c5Vlw0yo1q1L+4NnE/b0pcWO45bnfzYHSFp6CxkBcfDWQ6PAQMX8Lb4WLsNyIoJXD3lR9SgiWEWwUeLTwSv36baEwHfDW+CtQHKs+myPToOq4N6LAKacqdzlT9s6LFgrxb7UKLtT9U7wYrI5NYNf8tOXG4JDGLW5kZ0EvF75EEf0UiQo1m4sc5+jP2mN6vwFQW60K1TTA== 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: List-Subscribe: List-Unsubscribe: Instrument copy_from_kernel_nofault() with KMSAN for uninitialized kernel memory check and copy_to_kernel_nofault() with KASAN, KCSAN to detect the memory corruption. syzbot reported that bpf_probe_read_kernel() kernel helper triggered KASAN report via kasan_check_range() which is not the expected behaviour as copy_from_kernel_nofault() is meant to be a non-faulting helper. Solution is, suggested by Marco Elver, to replace KASAN, KCSAN check in copy_from_kernel_nofault() with KMSAN detection of copying uninitilaized kernel memory. In copy_to_kernel_nofault() we can retain instrument_write() explicitly for the memory corruption instrumentation. copy_to_kernel_nofault() is tested on x86_64 and arm64 with CONFIG_KASAN_SW_TAGS. On arm64 with CONFIG_KASAN_HW_TAGS, kunit test currently fails. Need more clarification on it. Link: https://lore.kernel.org/linux-mm/CANpmjNMAVFzqnCZhEity9cjiqQ9CVN1X7qeeeAp_6yKjwKo8iw@mail.gmail.com/ Reviewed-by: Marco Elver Suggested-by: Marco Elver Reported-by: syzbot+61123a5daeb9f7454599@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=61123a5daeb9f7454599 Reported-by: Andrey Konovalov Closes: https://bugzilla.kernel.org/show_bug.cgi?id=210505 Signed-off-by: Sabyrzhan Tasbolatov --- v2: - squashed previous submitted in -mm tree 2 patches based on Linus tree v3: - moved checks to *_nofault_loop macros per Marco's comments - edited the commit message v4: - replaced Suggested-by with Reviewed-by v5: - addressed Andrey's comment on deleting CONFIG_KASAN_HW_TAGS check in mm/kasan/kasan_test_c.c - added explanatory comment in kasan_test_c.c - added Suggested-by: Marco Elver back per Andrew's comment. --- mm/kasan/kasan_test_c.c | 37 +++++++++++++++++++++++++++++++++++++ mm/kmsan/kmsan_test.c | 17 +++++++++++++++++ mm/maccess.c | 10 ++++++++-- 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/mm/kasan/kasan_test_c.c b/mm/kasan/kasan_test_c.c index a181e4780d9d..cb6ad84641ec 100644 --- a/mm/kasan/kasan_test_c.c +++ b/mm/kasan/kasan_test_c.c @@ -1954,6 +1954,42 @@ static void rust_uaf(struct kunit *test) KUNIT_EXPECT_KASAN_FAIL(test, kasan_test_rust_uaf()); } +static void copy_to_kernel_nofault_oob(struct kunit *test) +{ + char *ptr; + char buf[128]; + size_t size = sizeof(buf); + + /* This test currently fails with the HW_TAGS mode. + * The reason is unknown and needs to be investigated. */ + ptr = kmalloc(size - KASAN_GRANULE_SIZE, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + OPTIMIZER_HIDE_VAR(ptr); + + if (IS_ENABLED(CONFIG_KASAN_SW_TAGS)) { + /* Check that the returned pointer is tagged. */ + KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN); + KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL); + } + + /* + * We test copy_to_kernel_nofault() to detect corrupted memory that is + * being written into the kernel. In contrast, copy_from_kernel_nofault() + * is primarily used in kernel helper functions where the source address + * might be random or uninitialized. Applying KASAN instrumentation to + * copy_from_kernel_nofault() could lead to false positives. + * By focusing KASAN checks only on copy_to_kernel_nofault(), + * we ensure that only valid memory is written to the kernel, + * minimizing the risk of kernel corruption while avoiding + * false positives in the reverse case. + */ + KUNIT_EXPECT_KASAN_FAIL(test, + copy_to_kernel_nofault(&buf[0], ptr, size)); + KUNIT_EXPECT_KASAN_FAIL(test, + copy_to_kernel_nofault(ptr, &buf[0], size)); + kfree(ptr); +} + static struct kunit_case kasan_kunit_test_cases[] = { KUNIT_CASE(kmalloc_oob_right), KUNIT_CASE(kmalloc_oob_left), @@ -2027,6 +2063,7 @@ static struct kunit_case kasan_kunit_test_cases[] = { KUNIT_CASE(match_all_not_assigned), KUNIT_CASE(match_all_ptr_tag), KUNIT_CASE(match_all_mem_tag), + KUNIT_CASE(copy_to_kernel_nofault_oob), KUNIT_CASE(rust_uaf), {} }; diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c index 13236d579eba..9733a22c46c1 100644 --- a/mm/kmsan/kmsan_test.c +++ b/mm/kmsan/kmsan_test.c @@ -640,6 +640,22 @@ static void test_unpoison_memory(struct kunit *test) KUNIT_EXPECT_TRUE(test, report_matches(&expect)); } +static void test_copy_from_kernel_nofault(struct kunit *test) +{ + long ret; + char buf[4], src[4]; + size_t size = sizeof(buf); + + EXPECTATION_UNINIT_VALUE_FN(expect, "copy_from_kernel_nofault"); + kunit_info( + test, + "testing copy_from_kernel_nofault with uninitialized memory\n"); + + ret = copy_from_kernel_nofault((char *)&buf[0], (char *)&src[0], size); + USE(ret); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + static struct kunit_case kmsan_test_cases[] = { KUNIT_CASE(test_uninit_kmalloc), KUNIT_CASE(test_init_kmalloc), @@ -664,6 +680,7 @@ static struct kunit_case kmsan_test_cases[] = { KUNIT_CASE(test_long_origin_chain), KUNIT_CASE(test_stackdepot_roundtrip), KUNIT_CASE(test_unpoison_memory), + KUNIT_CASE(test_copy_from_kernel_nofault), {}, }; diff --git a/mm/maccess.c b/mm/maccess.c index 518a25667323..3ca55ec63a6a 100644 --- a/mm/maccess.c +++ b/mm/maccess.c @@ -13,9 +13,14 @@ bool __weak copy_from_kernel_nofault_allowed(const void *unsafe_src, return true; } +/* + * The below only uses kmsan_check_memory() to ensure uninitialized kernel + * memory isn't leaked. + */ #define copy_from_kernel_nofault_loop(dst, src, len, type, err_label) \ while (len >= sizeof(type)) { \ - __get_kernel_nofault(dst, src, type, err_label); \ + __get_kernel_nofault(dst, src, type, err_label); \ + kmsan_check_memory(src, sizeof(type)); \ dst += sizeof(type); \ src += sizeof(type); \ len -= sizeof(type); \ @@ -49,7 +54,8 @@ EXPORT_SYMBOL_GPL(copy_from_kernel_nofault); #define copy_to_kernel_nofault_loop(dst, src, len, type, err_label) \ while (len >= sizeof(type)) { \ - __put_kernel_nofault(dst, src, type, err_label); \ + __put_kernel_nofault(dst, src, type, err_label); \ + instrument_write(dst, sizeof(type)); \ dst += sizeof(type); \ src += sizeof(type); \ len -= sizeof(type); \