From patchwork Mon Oct 14 02:57:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sabyrzhan Tasbolatov X-Patchwork-Id: 13834114 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 25176CF259F for ; Mon, 14 Oct 2024 02:56:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B28EF6B0093; Sun, 13 Oct 2024 22:56:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AD86B6B0095; Sun, 13 Oct 2024 22:56:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A0ED6B0096; Sun, 13 Oct 2024 22:56:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7D4606B0093 for ; Sun, 13 Oct 2024 22:56:46 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4E5411A0AC2 for ; Mon, 14 Oct 2024 02:56:33 +0000 (UTC) X-FDA: 82670694918.20.A8608C6 Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) by imf20.hostedemail.com (Postfix) with ESMTP id A11461C0008 for ; Mon, 14 Oct 2024 02:56:37 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ELEuC3C7; spf=pass (imf20.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.218.49 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=1728874462; 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=/8xPPGnp2eNdFzyDsH77udpuxb5hGHHiKYVsaBBQcww=; b=7yH7wLyWf0rOVMRivneUSxeN6l/gQWxb3dNldxqeKhSvwGZXziVo7pdrL0oUuU/86qYlXP wXCBbDZJ5qIwxX5Lx3+dp6MxI2dHwvkwP9I0UeGQBKGfIgbWXy0Vu1SJ3oIyyPeWAPGGzz U57WSQQJxrkBPcC7xKiAeNWCjEPyVKU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728874462; a=rsa-sha256; cv=none; b=0uHgFzC3J+tcPwwuuUv2XgOVG9YjplaxsNFarGyRedARL1yRG7jncEEF9EM1Ooe6lckXqT Z4tHrg7zS1AigNWlaoIQT9GwqNEuOo4FqUAhv/coRzXawaa/i0ItP4avAIsh3yVQvzaYUR n6Ne98gp2m893vYJIA+g0gWg5gfyV9A= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ELEuC3C7; spf=pass (imf20.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.218.49 as permitted sender) smtp.mailfrom=snovitoll@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-a99ffeea60bso138851166b.3 for ; Sun, 13 Oct 2024 19:56:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728874603; x=1729479403; 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=/8xPPGnp2eNdFzyDsH77udpuxb5hGHHiKYVsaBBQcww=; b=ELEuC3C7Ck0BQis0GOADCCAItqaCOesLsF8gcHLrbUtTiCgK7+Vcit9pvhMqITUpRi CzqP57wroQnMHgxQCpiiDHKiyEnGIPCQdLUrWWRBM6zCBomSTE79NMHdIc+m6Pe1Qwsl VFcIZS/FfHvlWiyjQAmj6Zr25QkHE9gvI9kc951abtuXpzrm9GX+9Hm1O/VBua453zKs qRiI6PXqPCpqodZqO8aNQ+5FQ9B40vPS4NTP7wwZ7nWVTQOCMczrZrshtEtwKvwEYiNG 530aSv07+DPU6mQjCkJR9iSprmthXeRrMclc5pWqD67hj+P2SSOISGgeI6ReWEoLVFhi r3ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728874603; x=1729479403; 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=/8xPPGnp2eNdFzyDsH77udpuxb5hGHHiKYVsaBBQcww=; b=AhvYNJ31h4G1qeEUPu7m0R+agjBHho6iaN5lYhIsid7kj8U8zJ3JMSOXMeHuH4OVZl 8PPCy/rS6Ih/pKDz/iApeeHMJOdeEWbouRM3sHYNz/S6jFV4eu3qJUY4ojGjr9WeIGWm reH22b8VpzFLJOlpRwgaUBJwtKX8wWaIWzHvscQMb9qZ6W6vV9p/fIJeN79hR+HN9otX 9SQRJfe0CRu0A5pfzVWVEaiN8Vy/6o/vPYmvm9Olpd0pmrWl9n8GyTb/zmWMNLjbf1+i 3fUsaENxvppI3WCv2uAP/sd8/zblGdNp+0Nm5GAfXNMFIqHmizHn2xq2778AQZnhTm00 T3nw== X-Forwarded-Encrypted: i=1; AJvYcCV7V9WrgDAXuxq9Ez05EPu0R60ksMVqSCBWAoyU4WT72330vLRzSNpvkLf+Wi9d8co8mg38574yZA==@kvack.org X-Gm-Message-State: AOJu0YzDEt6B9EjmjKHnUpXVvsX3+RlD+xECcKOajSxG8+F81XJPlz2k Dyw/+FLcCaZjRW0MtFJ8qT6OHxD/6ordTf8uwqsol9yP4NxzUZIF X-Google-Smtp-Source: AGHT+IGg2zTiSP9pkdThFwEPz88Rrc+thazAWWlSrFQkSIpC5/PWk+n9SRWCQsJl50F54xDsLVUSLg== X-Received: by 2002:a17:907:9488:b0:a9a:791:fb86 with SMTP id a640c23a62f3a-a9a0792046fmr282817966b.64.1728874602739; Sun, 13 Oct 2024 19:56:42 -0700 (PDT) Received: from work.. (2.133.25.254.dynamic.telecom.kz. [2.133.25.254]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a99ebdfbff1sm270501366b.39.2024.10.13.19.56.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Oct 2024 19:56:42 -0700 (PDT) From: Sabyrzhan Tasbolatov To: andreyknvl@gmail.com Cc: 2023002089@link.tyut.edu.cn, akpm@linux-foundation.org, alexs@kernel.org, corbet@lwn.net, dvyukov@google.com, elver@google.com, glider@google.com, kasan-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, ryabinin.a.a@gmail.com, siyanteng@loongson.cn, snovitoll@gmail.com, vincenzo.frascino@arm.com, workflows@vger.kernel.org Subject: [PATCH RESEND v3 2/3] kasan: migrate copy_user_test to kunit Date: Mon, 14 Oct 2024 07:57:00 +0500 Message-Id: <20241014025701.3096253-3-snovitoll@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241014025701.3096253-1-snovitoll@gmail.com> References: <20241014025701.3096253-1-snovitoll@gmail.com> MIME-Version: 1.0 X-Stat-Signature: p3qqm4jkrcwwe4syhdbeh6yd3now3dic X-Rspamd-Queue-Id: A11461C0008 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1728874597-753393 X-HE-Meta: U2FsdGVkX18n42e53hs9hz+GDbZm8afOMqdqFe+BHnZBE466YaNypCnz8k9gQGhXgjJlLRCBH7K1vPMLFWFAggG5p/llxDtWkjcK9UhUeRVpnVpoCFYsX0Mfz0pSn5q/JBStVD1rAZ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LTm8qqFcroFBi8DemuCHkUQHI6jcWZnG4HwcHvVL/77qjX1CMYOJZJAN44yQ3d1ao3lNQHz9eHL+wFEFuqMAJ0bvLOsSGdbEI4f5FYUSVqgjm+xfP6AOb85XkKW/pVAorAfk69OiBcQfg+3U/EwSFBHJhavBnvbZK3ExBp8pBIoCNt2Fl2M9wXtk5S8aPRn5gDpvwW97u2v/7jBlU5uYnY+mrr4fmIQLc4YUCwmNACNmg/R+06Nvao7wojFDRfise9MGQNHsMRfZZtuvP8sdXa+BLuwjxf8MA= 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: Migrate the copy_user_test to the KUnit framework to verify out-of-bound detection via KASAN reports in copy_from_user(), copy_to_user() and their static functions. This is the last migrated test in kasan_test_module.c, therefore delete the file. Reviewed-by: Andrey Konovalov Signed-off-by: Sabyrzhan Tasbolatov --- Changes v2 -> v3: - added a long string in usermem for strncpy_from_user. Suggested by Andrey. --- mm/kasan/Makefile | 2 - mm/kasan/kasan_test_c.c | 47 +++++++++++++++++++++ mm/kasan/kasan_test_module.c | 81 ------------------------------------ 3 files changed, 47 insertions(+), 83 deletions(-) delete mode 100644 mm/kasan/kasan_test_module.c diff --git a/mm/kasan/Makefile b/mm/kasan/Makefile index b88543e5c0c..1a958e7c8a4 100644 --- a/mm/kasan/Makefile +++ b/mm/kasan/Makefile @@ -46,7 +46,6 @@ endif CFLAGS_kasan_test_c.o := $(CFLAGS_KASAN_TEST) RUSTFLAGS_kasan_test_rust.o := $(RUSTFLAGS_KASAN) -CFLAGS_kasan_test_module.o := $(CFLAGS_KASAN_TEST) obj-y := common.o report.o obj-$(CONFIG_KASAN_GENERIC) += init.o generic.o report_generic.o shadow.o quarantine.o @@ -59,4 +58,3 @@ ifdef CONFIG_RUST endif obj-$(CONFIG_KASAN_KUNIT_TEST) += kasan_test.o -obj-$(CONFIG_KASAN_MODULE_TEST) += kasan_test_module.o diff --git a/mm/kasan/kasan_test_c.c b/mm/kasan/kasan_test_c.c index a181e4780d9..382bc64e42d 100644 --- a/mm/kasan/kasan_test_c.c +++ b/mm/kasan/kasan_test_c.c @@ -1954,6 +1954,52 @@ static void rust_uaf(struct kunit *test) KUNIT_EXPECT_KASAN_FAIL(test, kasan_test_rust_uaf()); } +static void copy_user_test_oob(struct kunit *test) +{ + char *kmem; + char __user *usermem; + unsigned long useraddr; + size_t size = 128 - KASAN_GRANULE_SIZE; + int __maybe_unused unused; + + kmem = kunit_kmalloc(test, size, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, kmem); + + useraddr = kunit_vm_mmap(test, NULL, 0, PAGE_SIZE, + PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_ANONYMOUS | MAP_PRIVATE, 0); + KUNIT_ASSERT_NE_MSG(test, useraddr, 0, + "Could not create userspace mm"); + KUNIT_ASSERT_LT_MSG(test, useraddr, (unsigned long)TASK_SIZE, + "Failed to allocate user memory"); + + OPTIMIZER_HIDE_VAR(size); + usermem = (char __user *)useraddr; + + KUNIT_EXPECT_KASAN_FAIL(test, + unused = copy_from_user(kmem, usermem, size + 1)); + KUNIT_EXPECT_KASAN_FAIL(test, + unused = copy_to_user(usermem, kmem, size + 1)); + KUNIT_EXPECT_KASAN_FAIL(test, + unused = __copy_from_user(kmem, usermem, size + 1)); + KUNIT_EXPECT_KASAN_FAIL(test, + unused = __copy_to_user(usermem, kmem, size + 1)); + KUNIT_EXPECT_KASAN_FAIL(test, + unused = __copy_from_user_inatomic(kmem, usermem, size + 1)); + KUNIT_EXPECT_KASAN_FAIL(test, + unused = __copy_to_user_inatomic(usermem, kmem, size + 1)); + + /* + * Prepare a long string in usermem to avoid the strncpy_from_user test + * bailing out on '\0' before it reaches out-of-bounds. + */ + memset(kmem, 'a', size); + KUNIT_EXPECT_EQ(test, copy_to_user(usermem, kmem, size), 0); + + KUNIT_EXPECT_KASAN_FAIL(test, + unused = strncpy_from_user(kmem, usermem, size + 1)); +} + static struct kunit_case kasan_kunit_test_cases[] = { KUNIT_CASE(kmalloc_oob_right), KUNIT_CASE(kmalloc_oob_left), @@ -2028,6 +2074,7 @@ static struct kunit_case kasan_kunit_test_cases[] = { KUNIT_CASE(match_all_ptr_tag), KUNIT_CASE(match_all_mem_tag), KUNIT_CASE(rust_uaf), + KUNIT_CASE(copy_user_test_oob), {} }; diff --git a/mm/kasan/kasan_test_module.c b/mm/kasan/kasan_test_module.c deleted file mode 100644 index 27ec22767e4..00000000000 --- a/mm/kasan/kasan_test_module.c +++ /dev/null @@ -1,81 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only -/* - * - * Copyright (c) 2014 Samsung Electronics Co., Ltd. - * Author: Andrey Ryabinin - */ - -#define pr_fmt(fmt) "kasan: test: " fmt - -#include -#include -#include -#include -#include - -#include "kasan.h" - -static noinline void __init copy_user_test(void) -{ - char *kmem; - char __user *usermem; - size_t size = 128 - KASAN_GRANULE_SIZE; - int __maybe_unused unused; - - kmem = kmalloc(size, GFP_KERNEL); - if (!kmem) - return; - - usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE, - PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_ANONYMOUS | MAP_PRIVATE, 0); - if (IS_ERR(usermem)) { - pr_err("Failed to allocate user memory\n"); - kfree(kmem); - return; - } - - OPTIMIZER_HIDE_VAR(size); - - pr_info("out-of-bounds in copy_from_user()\n"); - unused = copy_from_user(kmem, usermem, size + 1); - - pr_info("out-of-bounds in copy_to_user()\n"); - unused = copy_to_user(usermem, kmem, size + 1); - - pr_info("out-of-bounds in __copy_from_user()\n"); - unused = __copy_from_user(kmem, usermem, size + 1); - - pr_info("out-of-bounds in __copy_to_user()\n"); - unused = __copy_to_user(usermem, kmem, size + 1); - - pr_info("out-of-bounds in __copy_from_user_inatomic()\n"); - unused = __copy_from_user_inatomic(kmem, usermem, size + 1); - - pr_info("out-of-bounds in __copy_to_user_inatomic()\n"); - unused = __copy_to_user_inatomic(usermem, kmem, size + 1); - - pr_info("out-of-bounds in strncpy_from_user()\n"); - unused = strncpy_from_user(kmem, usermem, size + 1); - - vm_munmap((unsigned long)usermem, PAGE_SIZE); - kfree(kmem); -} - -static int __init kasan_test_module_init(void) -{ - /* - * Temporarily enable multi-shot mode. Otherwise, KASAN would only - * report the first detected bug and panic the kernel if panic_on_warn - * is enabled. - */ - bool multishot = kasan_save_enable_multi_shot(); - - copy_user_test(); - - kasan_restore_multi_shot(multishot); - return -EAGAIN; -} - -module_init(kasan_test_module_init); -MODULE_LICENSE("GPL");