From patchwork Fri Oct 11 07:16:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sabyrzhan Tasbolatov X-Patchwork-Id: 13832164 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 EC129CEDDBD for ; Fri, 11 Oct 2024 07:16:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F3526B0093; Fri, 11 Oct 2024 03:16:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A2CE6B0096; Fri, 11 Oct 2024 03:16:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 643066B0098; Fri, 11 Oct 2024 03:16:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 48B606B0093 for ; Fri, 11 Oct 2024 03:16:05 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DD3E8140EDE for ; Fri, 11 Oct 2024 07:16:00 +0000 (UTC) X-FDA: 82660462248.21.2461CE9 Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) by imf07.hostedemail.com (Postfix) with ESMTP id C25AB40004 for ; Fri, 11 Oct 2024 07:15:59 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=OuDl7cTL; spf=pass (imf07.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.167.53 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=1728630824; 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=XCjVoezSoJ9cXRJsoOufBJfIeNbu/4t5Ob362xE6LB4=; b=mDmvrTl8vuvPMDvHIOoQxcnLp+SuRYm2yTT91V4n3aBRD36yqTvpBWEWHI293XvHdXmoUY 3hs+Aiq5wOj3ruuJRoVV+OtkZsY6wRwamoAWzOJIthGqP18HiknXwRfCYmTrWEK30NfDRU y6/XRacXJNGsHbZ7/0JSbjHW6cglmXk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728630824; a=rsa-sha256; cv=none; b=n5/Q0fG0FNbrEuMpyBkSBrLudvRkK9g/w9C1yVTNb3yg1EJuXFhmRRtHx7/WuJAQ8p3w95 kdssIjnQOXsCXLv5wWhjQfI/DprUadX2NlKW0K1Ndf3vQndtGI1SM+lqRPsMyF4kb5cnBG nuBONSxohe3l7xwTLhgmNaSQW2VeWfI= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=OuDl7cTL; spf=pass (imf07.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.167.53 as permitted sender) smtp.mailfrom=snovitoll@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-5389fbb28f3so1620648e87.1 for ; Fri, 11 Oct 2024 00:16:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728630961; x=1729235761; 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=XCjVoezSoJ9cXRJsoOufBJfIeNbu/4t5Ob362xE6LB4=; b=OuDl7cTL7MgwYinAkHwQ2DB6PeL8thL9slSuUxsvezZ9CFmXgy5VANqaS5Z0QhbNR+ 57u9dUdU6cWZoX6IrDc5xLT0d6bQG72JSUH1CFjOnRQrNJ27t+eGOQyOUhQUF+/BtaXK QX1bF1aEzhMdAEuejfkb5iKLkrrscMDzDuQzPlMf0U1kHRv9cR9rcGj1EZSUQMYs8Qsj /kwTVOsQ2JlKzBKmBM3po5oHSXMADve8QZaXxDxLp4lhU5ob2fHhr5i77LwAjBLdAoiZ 9GRxOSdFdqfG+sP2IQyvWhdwmKW/UtaqksXfM2OoWyg4rRCt8AJOKoyE8yO9DTq98qix SX9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728630961; x=1729235761; 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=XCjVoezSoJ9cXRJsoOufBJfIeNbu/4t5Ob362xE6LB4=; b=FFytlcFpPnIqJ4NtnPsAO7JysuLoAOvGFGtvP1sL2DuP+5pgycyEy+zmj52g2C+Eqo 7p8mC/mNbT1WayplllCRA5jeRYcQvVRjnDS/Pj1pzwnB0ES8h15WvNNIRlJRUnUoOlet lT++/jn+DZkPINvEN0vyde1XYRnDucTJZFde+HlGbQGObFlh71ngPv6rzyXvcf/pawL3 kayF0tY2DuGh1ALnRTe/yDssokvNwBpGc4EHHksmYIazvxmwETNHhBlaohFV81cEKvug rjbHFu9PiT5MQUsvfUlvxRuJGO/pB4xbMPIAw7/eAgNtmLwTrRXGRngO86bXYihHamE0 iyww== X-Forwarded-Encrypted: i=1; AJvYcCV395KbOke5/zSrgzr6Ha3JMntayJQaakjhcuCrxzRNo1KWtRtzubFRemn6uI+vVBmG78pg5UCdmg==@kvack.org X-Gm-Message-State: AOJu0YxGNnDa1n/Uf+qAjPDCEZirNLbmGk+kcv5zLMUjAE+a8rNqhbJy 2He3UULnQhPYwylH1fTqrNoC4IKiQB83EJgeiDRSQw1P3EpCvlmY X-Google-Smtp-Source: AGHT+IHGHEvUs3xWI1jtReT4fvaMSO60WKGEZ8r2YOVj6BQ+NL8Ck2EfTqsTW723edt2foFUb3OniA== X-Received: by 2002:a05:6512:304c:b0:52f:36a:f929 with SMTP id 2adb3069b0e04-539c986474amr2004231e87.4.1728630960909; Fri, 11 Oct 2024 00:16:00 -0700 (PDT) Received: from work.. (2.133.25.254.dynamic.telecom.kz. [2.133.25.254]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-539ddf85cf4sm93946e87.68.2024.10.11.00.15.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 00:16:00 -0700 (PDT) From: Sabyrzhan Tasbolatov To: akpm@linux-foundation.org, ryabinin.a.a@gmail.com, andreyknvl@gmail.com Cc: glider@google.com, dvyukov@google.com, vincenzo.frascino@arm.com, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, snovitoll@gmail.com Subject: [PATCH] kasan: migrate copy_user_test to kunit Date: Fri, 11 Oct 2024 12:16:57 +0500 Message-Id: <20241011071657.3032690-1-snovitoll@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Stat-Signature: 38mg5ijmk36uwyn4qnif375hzkjuattm X-Rspamd-Queue-Id: C25AB40004 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1728630959-973014 X-HE-Meta: U2FsdGVkX1/pRE6fEDGSrO1i7IDaC6KbFUopEFebiHAdJSvr2D46EWkp/j428tJV9G4ecFiDiAavRDup5Dcw87nceGGYOzm6kgD2fZqFpykq3cvCfZ+TGPl46r8VApBSFsgU68Yh8axwBKMXHVDPFC5MrHaWxTqi91m2JDaFBeifMRq2yVlJ2Xr8KNKHX1L6lfQIkkFd/3QwnjUHDGgVxZpQyGsgMGxTHMCZUnD73uV+QcjKqtLbBRDJsaLkmRcyQ3myrB2SVxXqmhB8b+p8+QSikPbALNtuubcVC8DWgPONdbEonmwLI2d0V/H89rutcJ+UJkfitx0iW1fHn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LfJbQh0gE7i/DCIK2SCO7N6HJHAlhC2Z+FYQnKnLc/IiViuCpYvs3gLqkePuaAKE1Ooxs67IWeD+w4JLio6Ep0RfUHOIFruVF/E3fDQ1J9Jxjzn8iYgine7EpuySz1fyhy9s 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. In order to detect OOB access in strncpy_from_user(), we need to move kasan_check_write() to the function beginning to cover if (can_do_masked_user_access()) {...} branch as well. Reported-by: Andrey Konovalov Closes: https://bugzilla.kernel.org/show_bug.cgi?id=212205 Signed-off-by: Sabyrzhan Tasbolatov --- lib/strncpy_from_user.c | 3 +- mm/kasan/kasan_test_c.c | 39 +++++++++++++++++ mm/kasan/kasan_test_module.c | 81 ------------------------------------ 3 files changed, 41 insertions(+), 82 deletions(-) delete mode 100644 mm/kasan/kasan_test_module.c diff --git a/lib/strncpy_from_user.c b/lib/strncpy_from_user.c index 989a12a67872..55c33e4f3c70 100644 --- a/lib/strncpy_from_user.c +++ b/lib/strncpy_from_user.c @@ -120,6 +120,8 @@ long strncpy_from_user(char *dst, const char __user *src, long count) if (unlikely(count <= 0)) return 0; + kasan_check_write(dst, count); + if (can_do_masked_user_access()) { long retval; @@ -142,7 +144,6 @@ long strncpy_from_user(char *dst, const char __user *src, long count) if (max > count) max = count; - kasan_check_write(dst, count); check_object_size(dst, count, false); if (user_read_access_begin(src, max)) { retval = do_strncpy_from_user(dst, src, count, max); diff --git a/mm/kasan/kasan_test_c.c b/mm/kasan/kasan_test_c.c index a181e4780d9d..e71a16d0dfb9 100644 --- a/mm/kasan/kasan_test_c.c +++ b/mm/kasan/kasan_test_c.c @@ -1954,6 +1954,44 @@ 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)); + 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 +2066,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 27ec22767e42..000000000000 --- 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");