From patchwork Wed Oct 16 13:18:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sabyrzhan Tasbolatov X-Patchwork-Id: 13838465 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 77F12D1AD55 for ; Wed, 16 Oct 2024 13:18:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0ED916B008A; Wed, 16 Oct 2024 09:18:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 09DBA6B008C; Wed, 16 Oct 2024 09:18:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA8AE6B0092; Wed, 16 Oct 2024 09:18:21 -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 CCBD86B008A for ; Wed, 16 Oct 2024 09:18:21 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2AC35A0251 for ; Wed, 16 Oct 2024 13:18:03 +0000 (UTC) X-FDA: 82679518950.23.95B8304 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) by imf13.hostedemail.com (Postfix) with ESMTP id 7C36C20002 for ; Wed, 16 Oct 2024 13:18:10 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=B50aAktn; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.128.53 as permitted sender) smtp.mailfrom=snovitoll@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729084666; a=rsa-sha256; cv=none; b=SV/wmPjblvX2lRepiu2FnTwmPH6vdMvyXDj7NSQWLzSYp41TaVkzkn8CG1cIJR77ie6cDw Yhu2+g6le1Km/Yj79TmDRBHOCr5vNv/AwRAIg5tQnPlxOxQjacSZd0/7LOZxGKSJs4mzBe aeIitM9+/lWg0jA0dhlqjclKQPOk81Q= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=B50aAktn; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.128.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=1729084666; 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=CUXZk1z0JNB9S0Ip5rr+TNajHG6IasXkrD/X6fc8lvSYJMs4VJ7Ncnu/vPKCiB4Xc/7s7z xSrct22+ZQFmYsGgBXw+sYwW0HCoixvhdVCXc7sCcFW93XCLCFI8H63d1FGBkohvoEES9E zX8bzSQXLNrl/xdhMWEd7It4pX3n10I= Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-431548bd1b4so3783155e9.3 for ; Wed, 16 Oct 2024 06:18:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729084698; x=1729689498; 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=B50aAktnoifaJs9Ev1X8GglGhfhbqB2IPSREb/9xXHzmfNj/8Qs59U5utb7rYgsKmr AEfxm0tb5gG0rKqVCW3EkhsWgqmUk7kWk/zrIQ7Srw2/6ZDj7er/4boQIzRQF1UBYe2s P5znzwBYcdOcnUFk5QeChm6Qk3J+nSpUw1iZUSy+8kA3l1bOSI4XUhhXsudFg/g7JsEz q1TZ0VLLWwre2sA+L5hmeMMxNhRCh8IcBeTR6YKwdAFB7eEyHctzXJWkPpmclpdThjZA JdU6wzJkH2h498rRTbOjVKixxypx37J5k1p8xCPPKXPq0XAR2NWNzf9zjSSyO4fS4Gf8 c8QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729084698; x=1729689498; 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=rLCN6yuTOgxOSSnIiNf1cQnA4d3cvbgMfEOwnW9ga0ag6i/C6V9OOwip7TSlDEMlCl HDldVW3G0q1HMAgwpwLdLsSxpZbgTmaZC7v0BRNJhH1x63Z6INnOkeGCcKCHjH4jcYDh Qmu74UV7NGh5I9ld8XxpzkOkZJ1E9jA8JcCGP3oYNA+OdgFD1e1/DwbXRc7XbhFGG+z6 2FDhYGgOmdODPLqheXcexQpX8utJhrTaiRfzrjhTp2uswWhgWu1FGho7om+aAr01Lgj/ wHZdC5KfpOnT6iAHRg/uA/b6CgStJhIf2gxaWTlff65jtG4ImLq20E5FINWxppUim11s 8cOw== X-Forwarded-Encrypted: i=1; AJvYcCX+vUF9I6e3XEKA0guEoyEj793q8gNWSauu7cz4Y5eNyor6jB6Xyvwz8iwSMnxU9J0z7ISYW8VQCw==@kvack.org X-Gm-Message-State: AOJu0YzPDDZd0aZ3OHOZFIll27rh3iT0xpnxpvMH4KbWMkQoV//Bw2va v+lxHL9oqYsxztD8BjhZKukymyeSpJHWdljWPcQFSKd04g0TBVSe X-Google-Smtp-Source: AGHT+IH3FuLlFjmqTs3wizKlwLXV6vFTExlSBMXK4B6QAGh74FnpgDABFCIjNm+jC+pHYvNYiZORlg== X-Received: by 2002:a05:600c:4fd3:b0:431:5503:43ca with SMTP id 5b1f17b1804b1-43155035333mr7296085e9.28.1729084697677; Wed, 16 Oct 2024 06:18:17 -0700 (PDT) Received: from work.. (2.133.25.254.dynamic.telecom.kz. [2.133.25.254]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4313f5698aesm49612825e9.11.2024.10.16.06.18.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Oct 2024 06:18: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 v4 2/3] kasan: migrate copy_user_test to kunit Date: Wed, 16 Oct 2024 18:18:01 +0500 Message-Id: <20241016131802.3115788-3-snovitoll@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241016131802.3115788-1-snovitoll@gmail.com> References: <20241016131802.3115788-1-snovitoll@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 8ys6sjpes1b4xuz5jt6oupdc71wnrqs7 X-Rspamd-Queue-Id: 7C36C20002 X-Rspamd-Server: rspam02 X-HE-Tag: 1729084690-598289 X-HE-Meta: U2FsdGVkX1+PfswsPczRb87F8HO9pmrxDJX9ibul+1JAqp7oCwfgoZcCDxDJjqNAlkdEk81CP2wN/td/uKlVRZR+ZPMzClNrttgEAeJlowYZKicMp1Fk3103wZF6YCuEKLMW5TCs/p/hF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NKMNL1oIUdXqDieTe02UWekERR4pdZ0vEZhE8sPPhrbmFN5MViv7IgS+Knx4GctXTGYEhe2VuGC4Qcakds9Bt0lkjCK5Dt/+emhFLIUJVXRvBCuwDrmKka1SKtnLdUg7W6IzWdVhjRldeXh786wlmUQXxiTGCmh+u2ga7DJMLw5L3eY7o0dNOGEvu21BWAPQfn4Mp6+fCqI3FdMdZC8jwlftMMb8x0kJCBieJKMGiE5VuZJSJCyFMFlTby9JjkwSBmGVrAkJcUNEJnCrnVUM9DW/tSXU= 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");