From patchwork Sat Oct 5 16:48:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sabyrzhan Tasbolatov X-Patchwork-Id: 13823380 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 5E966CFA754 for ; Sat, 5 Oct 2024 16:47:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CADFF6B013F; Sat, 5 Oct 2024 12:47:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C5DCC6B02D5; Sat, 5 Oct 2024 12:47:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD75A6B02D4; Sat, 5 Oct 2024 12:47:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8EEAD6B013F for ; Sat, 5 Oct 2024 12:47:34 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 44D2614065C for ; Sat, 5 Oct 2024 16:47:34 +0000 (UTC) X-FDA: 82640129628.05.423A60C Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) by imf10.hostedemail.com (Postfix) with ESMTP id 648E4C0006 for ; Sat, 5 Oct 2024 16:47:32 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=c1cgbByD; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.128.48 as permitted sender) smtp.mailfrom=snovitoll@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728146811; a=rsa-sha256; cv=none; b=hNlvQbazx2XtVzrwohuWKEGo3oaMyZGHL9mr49JH6Nx0C2VRSs6zWK5RZAujSuauKrwoGn NOk1y5yY7cxgUm/gv3qPxnV8/kWeMcw3WDa70Jas/HgvThPKkujli5MV1lXY1Bsmlvkzr6 tc1r5rn0XTGt8ZqAtUGiFr1A1ewW5Nc= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=c1cgbByD; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.128.48 as permitted sender) smtp.mailfrom=snovitoll@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728146811; 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=q6F6OyhiHlCOdmPRfgY5RV4ZY7u4wQoO0FaQWpwh0ps=; b=VEKZTyjNq2r86p3EHK8WeDHsTgGhrS6onMBMrri38MUY+oihgr/F/0YtqaJ6QPJKI773vP f/awrTRMBXLVyt4G51ZVo+VleWACCwQwYEmCFPPkNeoj3WAn5BtVILc1/aXSYbSL5wbCXh NpFVIE8b/ZdCs77MQJ+Xnu4Utk2Zhc4= Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-42cb0f28bfbso27193925e9.1 for ; Sat, 05 Oct 2024 09:47:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728146851; x=1728751651; 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=q6F6OyhiHlCOdmPRfgY5RV4ZY7u4wQoO0FaQWpwh0ps=; b=c1cgbByDb+XbpCUhRtsalFfhfXhnQM2moxvBtPZGqj/6jbZ7EgSd1Baj7vb3WQ1hGT jOg6X2BpfwXgphUVPneEtuIITwyw5eZRZxuG6DsU3PyQI/HWtXC3cUIIBjLbsdBTOSia EtPtAeiyjInVt9uDzNpMcBrHBMf0c1J7X8L5KzIvdnuKjFUEoKKK3blHNUfOeAxRTA0D VV61utSN0AeuzNMOH6DHit/oL+PQ6ZfxEcXfV6M4C4tFjxk5vTw5I0ilrPS49prbVNe1 g1cfnR1XabK21ZnrWDejaBFOzx3QkEg9C3A+G5s6tL59ntexKnYYIvGHWcL3WZ4AOSiw tpTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728146851; x=1728751651; 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=q6F6OyhiHlCOdmPRfgY5RV4ZY7u4wQoO0FaQWpwh0ps=; b=t14h5RSrCQGqadV+y4Yu1BCnt3cSBuCfWML55kQzivkpT9flF++83d94yjnx/41DN7 1CBQopkDtAL+Vk7LUNJo4u5Z6DFoaeJetSe/UHW4cmgVgnB1QGIDyme/n/XwggsdKtPp 85tvIWqEnB092B5Le1Zjx0vBw4K1qQq1UOG/STV0EbIXVxMU37pW+qaCNiOGypLREPAS VTRHAa2ckIcs0+KPBQ7dNbidGZ16pHbIB41uEYzj20YNyQSfdWi07V1asr77N+Qf1wUG UOpeYhpTOHHk+OarkOyPuRi0Lxkk1Pr1VoEjJhvhsmU1LgRhN7/wnIyQpxBsgVESWB0Z aKRA== X-Forwarded-Encrypted: i=1; AJvYcCVW7xqVayUMylDpZbyUX5uCWAUb7f58yn/btzJYg9cGvyZFWn4BHZ2CCiFbV5Eg7pmTEv6CQWGwbg==@kvack.org X-Gm-Message-State: AOJu0YwoEnq0Qe9+1gJmq8hcVlcTdqPLL+/SB4bxv5A1F1mFzQFGq/vr eRr0YpNyHxG8Dl9ni0mYJi7qv7tJY4/Qe8hqqR+XevP6LJDOwsgj X-Google-Smtp-Source: AGHT+IGRMhdyR88SHH/XDkMpaLLrsydDkJXOsVYXtgoQI2RKA50PHZ8fgkrTQjY0KvtWK5o2wiVCdA== X-Received: by 2002:a05:600c:1f82:b0:42c:b750:19f3 with SMTP id 5b1f17b1804b1-42f859be4cdmr52608925e9.0.1728146850889; Sat, 05 Oct 2024 09:47:30 -0700 (PDT) Received: from work.. ([94.200.20.179]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42f89ec71aesm26481515e9.33.2024.10.05.09.47.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Oct 2024 09:47:30 -0700 (PDT) From: Sabyrzhan Tasbolatov To: elver@google.com, akpm@linux-foundation.org Cc: 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 v2 1/1] mm, kasan, kmsan: copy_from/to_kernel_nofault Date: Sat, 5 Oct 2024 21:48:13 +0500 Message-Id: <20241005164813.2475778-2-snovitoll@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241005164813.2475778-1-snovitoll@gmail.com> References: <20241005164813.2475778-1-snovitoll@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 648E4C0006 X-Rspamd-Server: rspam01 X-Stat-Signature: 7gazzwg5nu83xjax943i17j5ndpya3fr X-HE-Tag: 1728146852-913717 X-HE-Meta: U2FsdGVkX19Zwhj8xZ9j706U0mGWkQ756dxsOpehiuq819JI2xfGZ/EGBxaUG7ic+VicOgY2sDrFbLK0v45IdHlaykuw4tUvEB+9Pf+nvQdtGDQVHPFtAMzll1c6fI5V6vtYS+HwjPMJ64yGJdWuwGb+BAu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pInzv2NcVP15I0LLIcV+eeC1VBjWBKgw6pmob+BVR9xiX38nelqTUY6WmyEd/dcIkAntY2DTPAuvhwWOmkUrcde8OLrgR/nrqUkqo0EqD7s7D/7y1a32HKo1amYvBPdTi/j2+6FJq1/cy8l3lWIKzxtn48z57wudtMMitzXjCgnFnAByVK6syrYKlRJyGFE/0qtSEEqOqyK3a/Y6GCfMfT94Rs97rvKMQ9z3R517ror6pzznrxHvURmcSlo8hQZ7GYlNvfWI5BJa1GbG2GGpCStYDn7ctag0pzElNiIyq+QGO5Q38PxK61vhuMyH71RJaQz3GiL87ZlWVv3nWQ3ANbsj0sNUIvn39t8UHVsGXY/U0Q11o8UYC3+wMplVcYs4KRyCtn1KmGFHUtysQ7YwR1B11HV6IQl3Bw+wu07Jp3pyPjO8vhujfcmZRWVf5LAwTRr23S5QhVjRgxg== 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() for the memory corruption instrumentation but before pagefault_disable(). 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 - currently, disabled in kunit test. Link: https://lore.kernel.org/linux-mm/CANpmjNMAVFzqnCZhEity9cjiqQ9CVN1X7qeeeAp_6yKjwKo8iw@mail.gmail.com/ 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 --- mm/kasan/kasan_test_c.c | 27 +++++++++++++++++++++++++++ mm/kmsan/kmsan_test.c | 17 +++++++++++++++++ mm/maccess.c | 7 +++++-- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/mm/kasan/kasan_test_c.c b/mm/kasan/kasan_test_c.c index a181e4780d9d..5cff90f831db 100644 --- a/mm/kasan/kasan_test_c.c +++ b/mm/kasan/kasan_test_c.c @@ -1954,6 +1954,32 @@ 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); + + /* Not detecting fails currently with HW_TAGS */ + 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); + + 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); + } + + 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 +2053,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..a91a39a56cfd 100644 --- a/mm/maccess.c +++ b/mm/maccess.c @@ -15,7 +15,7 @@ bool __weak copy_from_kernel_nofault_allowed(const void *unsafe_src, #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); \ dst += sizeof(type); \ src += sizeof(type); \ len -= sizeof(type); \ @@ -31,6 +31,8 @@ long copy_from_kernel_nofault(void *dst, const void *src, size_t size) if (!copy_from_kernel_nofault_allowed(src, size)) return -ERANGE; + /* Make sure uninitialized kernel memory isn't copied. */ + kmsan_check_memory(src, size); pagefault_disable(); if (!(align & 7)) copy_from_kernel_nofault_loop(dst, src, size, u64, Efault); @@ -49,7 +51,7 @@ 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); \ dst += sizeof(type); \ src += sizeof(type); \ len -= sizeof(type); \ @@ -62,6 +64,7 @@ long copy_to_kernel_nofault(void *dst, const void *src, size_t size) if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) align = (unsigned long)dst | (unsigned long)src; + instrument_write(dst, size); pagefault_disable(); if (!(align & 7)) copy_to_kernel_nofault_loop(dst, src, size, u64, Efault);