From patchwork Fri Oct 11 03:53:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sabyrzhan Tasbolatov X-Patchwork-Id: 13832025 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 9DF77D2446B for ; Fri, 11 Oct 2024 03:52:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DBDD26B0083; Thu, 10 Oct 2024 23:52:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D6E166B008C; Thu, 10 Oct 2024 23:52:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C35486B0092; Thu, 10 Oct 2024 23:52:18 -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 A614A6B0083 for ; Thu, 10 Oct 2024 23:52:18 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 0BE181A0D69 for ; Fri, 11 Oct 2024 03:52:11 +0000 (UTC) X-FDA: 82659948714.28.73D1B83 Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) by imf24.hostedemail.com (Postfix) with ESMTP id 0D763180015 for ; Fri, 11 Oct 2024 03:52:14 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HjgJ+3fj; spf=pass (imf24.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.167.46 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=1728618599; 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=w0fnDdAXdFFiAdPLRHXqXwWbKuYcXlWeTLq43KdMa6k=; b=aBxaiaczMoQjz4OsLix8Og8iiXghs6F4JSeVFxDbF77j6KnufYxKB2NuIjQzsGylj7Mh8b H2a043qKSyHW1KcZ4erg9a2M7L0Y9xs8dQ+7voyAm5YBJLUt34WO6EgiYccSDKJnsjS+5p 7eCYvhrwgGBQp6Grz3lDDD9QbF1u2Ag= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728618599; a=rsa-sha256; cv=none; b=M7NdqRR743jM4BBABjCnkn5s+X5uCCzlTrJ2lHTwSHac1z8SyWkXdpFh4MuM14vszYgjiw CETfypECUeYyQ13HJzxAj3GgPIJdjf01wGA2iQYAZglnQj/ZM95QHSdVN/Xe5jYUMidR2j jRsZuTyRjfb8tsXSyA2l7eMpKXtvO8o= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HjgJ+3fj; spf=pass (imf24.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.167.46 as permitted sender) smtp.mailfrom=snovitoll@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-5398e58ceebso1506937e87.0 for ; Thu, 10 Oct 2024 20:52:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728618734; x=1729223534; 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=w0fnDdAXdFFiAdPLRHXqXwWbKuYcXlWeTLq43KdMa6k=; b=HjgJ+3fj28siZzn9MBQc6Cmr2dl+8kOkCBpYS+4/oVDhkJZ9kFa2EvwwaSBCXnrzBW ZnOeWkaaToYkNLIH8VKkQVSdRgVPevE+7cMcdPRZurLwy4cLf8VYfInRSnB2cezSQAMy 5vI5EgCHtTt+Jk9qplxiFwk4V/f4BdFprMAmDfx01bavONi2r+Zjyn8e4ScjakP1HdO+ YmGU44Rf5eCLZBI+/6/dYquLPBlR6PtHxKx8oOoQwzqNBJnlCdyc2hRDYtq3KD4RlsPh fl3NoUMRrmWmNyv77pnS4g6oB5KENWxcTj9OH6trWbunCT36hmS/NqfIxwYQlH/jNqza Oj+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728618734; x=1729223534; 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=w0fnDdAXdFFiAdPLRHXqXwWbKuYcXlWeTLq43KdMa6k=; b=VgykxMivyChzielA7np/Mwq0GcjO7Y5npOiz/m0RmF+K554aM5C8z9tnrq1gI8aEHA y9lN5uLSSSlVBluZlk6iRE38qDjvExRXmMweIdd+JE3FZvAVnepn6qkHAXNvi2CEPgSI VNC9P32nE5beMb+HZ6bKl7i7PZkPl7Ilh/DS6ReD25sukC6kBGQ2xCSMn631LNu+02x/ h6bAKPe8Wpz1TOiSKoYe0gapOr0P8McEck+rZStBIGPpj32z2UiZ+bUfxIqc0r7wmSIM rV4e5ybPykM4iRjmJ9HMFIVh2byc2cD5BeeG39cVajhut5DVgdP30irBva8HHmQ4mQ1m 7g0g== X-Forwarded-Encrypted: i=1; AJvYcCX1TalJXwAJV2peeMAEQJJ8bf5ku1Drl8/iGGq5hQwr3A5PqY+97IqPio+vgt0ymf1UkaKCyi+R6A==@kvack.org X-Gm-Message-State: AOJu0YwgHm5JDl9NR5Z4xLvavfWp3z+tRMWhN+wvt+4+Gw3dLbEfmdat DFep6c1hY6YS4M6HAm6nMnqzh9gJAeIP6l0sPjBTB6WeEIULy3po X-Google-Smtp-Source: AGHT+IG2r9G8kehETMTOGptVXRccibX8e8/dju6uDVYVxfbmv/ZQsTG+3vVhfke6Bb1PYxuY3HOZUQ== X-Received: by 2002:a05:6512:3a91:b0:539:9135:698c with SMTP id 2adb3069b0e04-539c9881bc8mr1605719e87.16.1728618733895; Thu, 10 Oct 2024 20:52:13 -0700 (PDT) Received: from work.. (2.133.25.254.dynamic.telecom.kz. [2.133.25.254]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-539df2fe2d5sm36383e87.61.2024.10.10.20.52.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2024 20:52:13 -0700 (PDT) From: Sabyrzhan Tasbolatov To: andreyknvl@gmail.com Cc: akpm@linux-foundation.org, bpf@vger.kernel.org, dvyukov@google.com, elver@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 v6] mm, kasan, kmsan: copy_from/to_kernel_nofault Date: Fri, 11 Oct 2024 08:53:10 +0500 Message-Id: <20241011035310.2982017-1-snovitoll@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 0D763180015 X-Stat-Signature: 9sbrpt7tjaxweopdbc5oobhqweiejztd X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1728618734-814103 X-HE-Meta: U2FsdGVkX19P5jVZLqGE+aSJu7LlKGg3q3JJg49FFWAL8DSEW1HmuIHdQNxGux+FhKGd/SfFvyWiyDa9+LtV/vUI9O00aGx6d8HxOkj1Vl3Qxad7VkhBAW3nydF+Fc+j1S4DfuGKeCkylk5BrO2mcz0UTH1CiTkDkwKFSpXGzjS2gB2ykbCp7B0oQBmBuIrWgnkaGN+05O4oru0yc6lrc2Z8tr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vD0IzbMqgSmAQ2UEmPXpvD+0zBggsfTqPrwojPZz4zix5BmTVILgD6NFOb4OF2073+tChus/BB5B4ZLpskHDWswZIXlyZ1/cqWSAst4pqJtg4uaGHNnjGRaGomqkKVGbzvVtbCqYvzOXlDgB6sgEJgoNMa/OGy5u0Z4NYu5K1sXcDMhynCKY2zCm2XySSjHyOR40U1n7av1LQlkRciacRTiN+YRpnZTQy2Ezbpa9+zYwo4Num3s+brG/6cQiKjgY1FXcXSbba+or8z35iVEcBZXvlrxEv6YinM7MAK6bmBuT/TBYMsNUDC9oYKtH64HgCsHhxma3Dlf8F9nM8FYrw0n/quWQepxIKGPuKyvE7DBIN5cHq5lgHC0lFtg== 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 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 Reviewed-by: Andrey Konovalov Tested-by: Andrey Konovalov --- 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. v6: - deleted checks KASAN_TAG_MIN, KASAN_TAG_KERNEL per Andrey's comment. - added empty line before kfree. --- mm/kasan/kasan_test_c.c | 34 ++++++++++++++++++++++++++++++++++ mm/kmsan/kmsan_test.c | 17 +++++++++++++++++ mm/maccess.c | 10 ++++++++-- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/mm/kasan/kasan_test_c.c b/mm/kasan/kasan_test_c.c index a181e4780d9d..716f2cac9708 100644 --- a/mm/kasan/kasan_test_c.c +++ b/mm/kasan/kasan_test_c.c @@ -1954,6 +1954,39 @@ 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. */ + KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_HW_TAGS); + + ptr = kmalloc(size - KASAN_GRANULE_SIZE, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + OPTIMIZER_HIDE_VAR(ptr); + + /* + * 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 +2060,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); \