From patchwork Sun Oct 13 18:20:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sabyrzhan Tasbolatov X-Patchwork-Id: 13833407 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 5E510CF2576 for ; Sun, 13 Oct 2024 18:19:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7ECFD6B0082; Sun, 13 Oct 2024 14:19:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 79CFB6B0083; Sun, 13 Oct 2024 14:19:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 63D066B0085; Sun, 13 Oct 2024 14:19:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 45F506B0082 for ; Sun, 13 Oct 2024 14:19:22 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CD4AC8080B for ; Sun, 13 Oct 2024 18:19:15 +0000 (UTC) X-FDA: 82669391238.04.B8EAE92 Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) by imf30.hostedemail.com (Postfix) with ESMTP id 2EBEC80008 for ; Sun, 13 Oct 2024 18:19:09 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Z0JZdU/I"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.218.53 as permitted sender) smtp.mailfrom=snovitoll@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728843456; a=rsa-sha256; cv=none; b=V5t0LBtMPges4Y6GBB2l1+4ZD4zIxVgmKmeR0/sSfhJiVMKgSxYgyndqtBXvY+j+uc9/Ot F+uCvXBSEbXAUbrFZda8Mp9a0yYmjqVMS/rbjvjFetZqxUTAvHu7ysWoYb/E8lSctfygy3 1i/RMLqNtIHeewWUdxoI3sgu7fYiIZg= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Z0JZdU/I"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.218.53 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=1728843456; 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=X56ki4zUkTbfkScVrmia1mDMQKgiUsSq+BtzwCXhyN8=; b=6lLMDTl8PsbHuEq8PMghDVqNU3EM7MRuhMRJS8KxOTYE8pJqmtkqpYb83EtX7ZeEqgu+C8 BfUbsFZCz9IN5snOijhnkZQGeNevzWT7YrpFN8RZ/7jbO0NfLLPALlV9BsjRs691pD4qzt S1+njeq70NKmCASF1h+Z1dbZsoRjxTk= Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-a99d1b7b5f5so238535466b.0 for ; Sun, 13 Oct 2024 11:19:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728843558; x=1729448358; 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=X56ki4zUkTbfkScVrmia1mDMQKgiUsSq+BtzwCXhyN8=; b=Z0JZdU/Icyv/j/5gzbjYeqR7oV9YDsCU6YqAFBpqDZdPGabnExdAWLb4/RJLXR1YHG N24+gaLsA8RQqCnPNO//vsrasXlPYouIQMihzXaprs9WzDqbkGesjvmP/OStkBz++tI4 Cyn43Cg1FaZv6/xHnKS8ll9IKm1ldfkGLelBNKHash18Cjbyht1AGHFjCI2tOHOKWcEi s3DGtIC1d8WLC+mppZX0DPCV81hf2XifgNfBUB/d8v8jvCwf9H+cWZQlm2scIfJDf+jc q9pftLS5+97xm4N91bCvG5wSR45ZulAM6pej49TO6bmT6QmKkru+orhHEe7WDtvi6N8i JKSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728843558; x=1729448358; 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=X56ki4zUkTbfkScVrmia1mDMQKgiUsSq+BtzwCXhyN8=; b=Egto3fg0k3MvfjC7z54xuxZBsSEE9VME/AE967xEth+Pe+dsmvhpnV6Cutzdc2WvFs 4qlcS9XTZctVXS5OoIuDpJl1/l22XEmU+MP1TliTRMZIFrIK06KhTg+Apcza0mEs1DC1 ByiNKhBbRe0dDqVmBGmO/5JRsrz8/bTPOPv6WbYzeefHSpqVA8PTX4HdYVCoE2bDQe/J EfyLr9hUWRzYQtsolgnyr11b6k5jmOfmompN69m6/gkQPEsvwrHOry0gOrRMLzbhW0qz +0JmsHnpQWfJ+J48n22Y2v0eRe05Ne06AbqhZ/XumL7kYCnlPsoN3c0rchs6WRw1MSRW as/Q== X-Forwarded-Encrypted: i=1; AJvYcCW0CeaWKZDoUWtunO62M4Czy52BB/i4EHAowq5ZYlejFigxf1DOds4DI3EdSO/bf0Y3L0kTebdd+A==@kvack.org X-Gm-Message-State: AOJu0YzFY7Kgud8Jqg9xu9W3FWGK+9S2K3dXSwpFkphhM2/DkEwRRmca 7kyR1vUrPow1WslQcGZBFgT/NOCmIDKky/Pc29p3ITHf2UPR4Fn1 X-Google-Smtp-Source: AGHT+IEPuqRk1fF+DzXKNYdEqvVLvn9/MKGMqgO/l9jzR0cpo8qxe+FtFoILPmStmwoQA5yi91dqsQ== X-Received: by 2002:a17:906:fe41:b0:a99:c075:6592 with SMTP id a640c23a62f3a-a99c075952emr779610666b.56.1728843557929; Sun, 13 Oct 2024 11:19:17 -0700 (PDT) Received: from work.. (2.133.25.254.dynamic.telecom.kz. [2.133.25.254]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a99e57f763bsm249937066b.85.2024.10.13.11.19.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Oct 2024 11:19:17 -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 v3 2/3] kasan: migrate copy_user_test to kunit Date: Sun, 13 Oct 2024 23:20:16 +0500 Message-Id: <20241013182016.3074875-1-snovitoll@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 2EBEC80008 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 1qn5tk99p5yn3cwhwmkf1d6s6npatxf8 X-HE-Tag: 1728843549-205528 X-HE-Meta: U2FsdGVkX1+B2sG3vvPW+N6ofnMmCIBMMulo4DvSikbNZFKtlSJZqAH/+6gsWPhtAodSzM2DM6BIprG7lvTatGZYlpLohNHJRYsoTW9K+uyae4rvc2KR6JqMIf8Zen/rvMlh8TbHBYSxNO1wsMMSlVhsxt4o5BOPcw3YIaiftrtSaNMIs4YnNs8TM+I3W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bhYFEG8VIiqLPPUP856GAeWSj7j6pnMJuReKMqe+dpBaFy1ffG/gX4PwCbqag3fmYIK62OxRlV+41DtxfSiGOOPgSyKchuKQGBRMKjeGplbrOHC4Hcp7tEEliwnL4N72OYzcOjL8dF3UnrZnngqPb72MbwBdPOLQy7QCMN9mAkgk5MKe1f8L7HWLXc5KUCS+ROxETzPRs5E6Nzu5yErFSKMe2j+ibqQLjneIca+hJAlMTlbnel/pP1zsvZc/TbcDep6Q= 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. Signed-off-by: Sabyrzhan Tasbolatov Reviewed-by: Andrey Konovalov --- 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");