From patchwork Fri Sep 27 15:14:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sabyrzhan Tasbolatov X-Patchwork-Id: 13814404 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 6E956CDD1BA for ; Fri, 27 Sep 2024 15:13:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C3E9B6B00FA; Fri, 27 Sep 2024 11:13:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BECB36B00FB; Fri, 27 Sep 2024 11:13:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB45A6B00FC; Fri, 27 Sep 2024 11:13:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 89D2E6B00FA for ; Fri, 27 Sep 2024 11:13:58 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 323E880E1B for ; Fri, 27 Sep 2024 15:13:58 +0000 (UTC) X-FDA: 82610863356.14.94B621A Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) by imf29.hostedemail.com (Postfix) with ESMTP id 5FD21120024 for ; Fri, 27 Sep 2024 15:13:55 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QyOGIZlS; spf=pass (imf29.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=1727449913; 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:references:dkim-signature; bh=C2yd0eA80h4rdtqxp/kh9iCrWOEyI0g4hsGihttS/Wc=; b=08R63jrVzW2Azr9dq9b28QqG/jwNcXtyK4G0JA7QJQ+SQM+G/r7STzCcYT17yZwL81mjJS gvKUZeaEfvmGzlfErlUBcR4+l1I6MqjpJpTmiJmmcbD0m1tO5L6hs9TaH1vj3zrDBf5yMD UfeUvDpxqD8zS3sxnAzbRL1LzVsqmZw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727449913; a=rsa-sha256; cv=none; b=Y0WVMRD05IL9SSMVX4yg1IV4D8IOGoO0LPCcVRyqo8sg+GjoY9ZZLS//EGwnphFnCimVDj W+bnbr9tEIEKQ72/h3EZvLlCTt+xbMqA/J6oQz4sGC40DhgXaRDk+o1MFf1yV7pUzvTej2 /o+3RhbNC3UkCvUfNzT4Xz1eTDXQPuM= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QyOGIZlS; spf=pass (imf29.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-53659867cbdso3702324e87.3 for ; Fri, 27 Sep 2024 08:13:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727450033; x=1728054833; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=C2yd0eA80h4rdtqxp/kh9iCrWOEyI0g4hsGihttS/Wc=; b=QyOGIZlSLWwZkDujv6E2IvyT6VGo5ZC2x9kIK1pQbJX7Y3Xp+cErOp7q5AaodB+brS 8e3AUJ0fJJH/weSOfsmVLOXraG0uPnz44+WDoIF1eUuQRn9aPNOcauTCzDWBY+SznDD5 aDTBsLn+F0qzROrHDygWRj1EhcYShkh15eYRceUE32GXSb1omm0oqOUxByTpQJ6MkZyJ 83HKoNLLcQx9f4bfKz6pasUmtuXh5g2oemk2bCuX8QYn6fOTIqy34P1u/IUv7y7urk50 X1IFWhkIQsxWWiLvdLFIw1SQxAoFarWmTGmTV/Qg512E+esIwkGKLr+oAl1hTRhFYIYC AToQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727450033; x=1728054833; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=C2yd0eA80h4rdtqxp/kh9iCrWOEyI0g4hsGihttS/Wc=; b=nImrpTn4s4HSIHIoo3di3J597qswJL+lvNSV59bNa2Z+MvvLG13zmLfdi97mLUgefC dfqWqAKrZT9XToOqfd2OFD9TZghHs7X72GCkCZwauYIMSYkPjkKMM248ZWTr+elFDUyd hLuffux/aG7B+A+gsBA6GiVoS5623hcBSvkkhKXq9WOg3SdG4Wi+8fwd+M+Z8ML4NoNq tp44PhbxtuGQ2y8XF6QMyxmVKeBdOe2mJX2C67gx0kw5od6OaUiLPcXUGDSVytd9LsAC +xo/uH5RLraM2mOsUWayZk6C/hSzgf4EaohHWI2fGvPrm9BrvVfq0HnqBvyZn/0B5fio SRqw== X-Forwarded-Encrypted: i=1; AJvYcCUpEemFl9sHtAgGsttEMel6AjYJjudGkTkirjX/GEwVoQs4tdm2Dv8jodwrAgcRi5zFR1/jUbNIMA==@kvack.org X-Gm-Message-State: AOJu0YyVKeAszXPkVrjjDALuYCf06Yx1sPW59JX5zOm4EzaG9HaIwqGL HBooxID3GJ2ttKiYc46jKSjj6QlHKocORH3aAmGp/ODzR6lU0+c9 X-Google-Smtp-Source: AGHT+IEjyfmt6pFAwEcM9NxXtldfiXXSEydABqji67UTTHosRYP72Gqr3CCMuHJxC/aC30wzCh2APA== X-Received: by 2002:a05:6512:3343:b0:531:8f2f:8ae7 with SMTP id 2adb3069b0e04-5389fc46b2dmr3479365e87.25.1727450033114; Fri, 27 Sep 2024 08:13:53 -0700 (PDT) Received: from work.. (2.133.25.254.dynamic.telecom.kz. [2.133.25.254]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-53979921f54sm268143e87.152.2024.09.27.08.13.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 08:13:52 -0700 (PDT) From: Sabyrzhan Tasbolatov To: ryabinin.a.a@gmail.com, glider@google.com, andreyknvl@gmail.com, dvyukov@google.com, vincenzo.frascino@arm.com, akpm@linux-foundation.org Cc: kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, snovitoll@gmail.com Subject: [PATCH] mm: instrument copy_from/to_kernel_nofault Date: Fri, 27 Sep 2024 20:14:38 +0500 Message-Id: <20240927151438.2143936-1-snovitoll@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 5FD21120024 X-Stat-Signature: 1mx699sq3m8rgwwzryinojccs7jy31er X-HE-Tag: 1727450035-637244 X-HE-Meta: U2FsdGVkX1+qPWCRKJNVT0H5qVmHHmEisKVlN+HNc7pLsPuPidwCWadqECP1mJkmCi0YYNyVKckXPf1/txk8dNSgo7bvLyChbXA6C9veU5SRM/Cxf6mhGhsYNfuCDe55rrUaqOKr3FV0UeEhNdSfWUUv69RcSBe7MiPFv+zabDxJvOfsjYHvN0G1kYZlY9vu1wmR4o9nWr8quCifGE9+GGMkWYcY99+Ho+fHNFnvq8kk5AY/Emiy5wUMtDPNOzmPcRp4yNGU5DmJyBaJrtHmpnjHnCg8xO7xIjUo6ZbKN7Bzm+xBmQ33DSU3u0RIb29z4YdcYB4X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H558qGQr7jbKy1WXMP6NMV7lfUd9h70jbQfoW+Ver43d4gx3T83qA9S+g1rOzTELTzebVYa9uE7A2TcoIHu94Kcuaem847uYpgpoihYBGuR6AySnXxT5U= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000070, 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(), copy_to_kernel_nofault() with instrument_memcpy_before() for KASAN, KCSAN checks and instrument_memcpy_after() for KMSAN. 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. Reported-by: Andrey Konovalov Closes: https://bugzilla.kernel.org/show_bug.cgi?id=210505 Signed-off-by: Sabyrzhan Tasbolatov --- mm/kasan/kasan_test.c | 31 +++++++++++++++++++++++++++++++ mm/maccess.c | 8 ++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c index 567d33b49..329d81518 100644 --- a/mm/kasan/kasan_test.c +++ b/mm/kasan/kasan_test.c @@ -1944,6 +1944,36 @@ static void match_all_mem_tag(struct kunit *test) kfree(ptr); } +static void copy_from_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_from_kernel_nofault(&buf[0], ptr, size)); + KUNIT_EXPECT_KASAN_FAIL(test, + copy_from_kernel_nofault(ptr, &buf[0], size)); + 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), @@ -2017,6 +2047,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_from_to_kernel_nofault_oob), {} }; diff --git a/mm/maccess.c b/mm/maccess.c index 518a25667..2c4251df4 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); \ @@ -32,6 +32,7 @@ long copy_from_kernel_nofault(void *dst, const void *src, size_t size) return -ERANGE; pagefault_disable(); + instrument_memcpy_before(dst, src, size); if (!(align & 7)) copy_from_kernel_nofault_loop(dst, src, size, u64, Efault); if (!(align & 3)) @@ -39,6 +40,7 @@ long copy_from_kernel_nofault(void *dst, const void *src, size_t size) if (!(align & 1)) copy_from_kernel_nofault_loop(dst, src, size, u16, Efault); copy_from_kernel_nofault_loop(dst, src, size, u8, Efault); + instrument_memcpy_after(dst, src, size, 0); pagefault_enable(); return 0; 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); \ @@ -63,6 +65,7 @@ long copy_to_kernel_nofault(void *dst, const void *src, size_t size) align = (unsigned long)dst | (unsigned long)src; pagefault_disable(); + instrument_memcpy_before(dst, src, size); if (!(align & 7)) copy_to_kernel_nofault_loop(dst, src, size, u64, Efault); if (!(align & 3)) @@ -70,6 +73,7 @@ long copy_to_kernel_nofault(void *dst, const void *src, size_t size) if (!(align & 1)) copy_to_kernel_nofault_loop(dst, src, size, u16, Efault); copy_to_kernel_nofault_loop(dst, src, size, u8, Efault); + instrument_memcpy_after(dst, src, size, 0); pagefault_enable(); return 0; Efault: