From patchwork Sat Sep 21 07:10:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sabyrzhan Tasbolatov X-Patchwork-Id: 13808779 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 C7820CF9C68 for ; Sat, 21 Sep 2024 07:09:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BE60C6B007B; Sat, 21 Sep 2024 03:09:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B95C86B0082; Sat, 21 Sep 2024 03:09:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A36536B0085; Sat, 21 Sep 2024 03:09:34 -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 849A56B007B for ; Sat, 21 Sep 2024 03:09:34 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CFB901C50A7 for ; Sat, 21 Sep 2024 07:09:33 +0000 (UTC) X-FDA: 82587869826.16.7FBE810 Received: from mail-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) by imf13.hostedemail.com (Postfix) with ESMTP id 00E5420008 for ; Sat, 21 Sep 2024 07:09:31 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=W+Zzf3Sb; spf=pass (imf13.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.208.176 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=1726902419; 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=+ohAzvebGtlWdpIGNEivr1Zt3bErXvsBaqlk21HjfVY=; b=J+JvPFoglQwWiIW70pXBVd/huTjHzKoHb7dGlkYXG8zWqvE3BU/ohFCrtpznGSy5pqedq/ aU3fVv34wOj/Xx9lBScex5xp//T27j4Hqf5Kvk6hCHgq7mNxB5AsGaMooSGSVCv6e3hTxo zrawBFA4hiYuzf4/x8DCggH91mWXbHs= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=W+Zzf3Sb; spf=pass (imf13.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.208.176 as permitted sender) smtp.mailfrom=snovitoll@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726902419; a=rsa-sha256; cv=none; b=lTBP5Z+aCBqEtrEyMG+PRpBrCuuy13rNv3pgo8/jS/FIL6UrYPfAHMYFATPGxJrtYrmgDj 7fAAMS+xoEcG+eSsUjtK+PbcAjoGnWv0Lw1tkNrWh5BYjn+etF5mAWS6TEM4A2HI/u+Jr/ HI+ZT10D6L1rnnUiyBS6D6aXqPnbEVM= Received: by mail-lj1-f176.google.com with SMTP id 38308e7fff4ca-2f7529203ddso35541111fa.0 for ; Sat, 21 Sep 2024 00:09:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726902570; x=1727507370; 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=+ohAzvebGtlWdpIGNEivr1Zt3bErXvsBaqlk21HjfVY=; b=W+Zzf3SbAYGxcvQawcrCdPKaE5KhltKAD7cYoE6eXeYDJ/Wz8gtz2zrv0wwOHZjevi xwq7W4/aYYGTlJf9OxDK0BgCopSSBtGlF+A11Ps+IDRj81OHgLXC5ELmbwcrFEDldUMb LHgVNbrk3B+4N3NfnC0c2iUs4sJZRBHBRvOwwdCaavm0zZWxwKVF8WIgnD+roopVvvWv nK3qEoeb/qWoYDgBcOj5umFnH4Q7SDSAVP5qUi5xvXt82FKeCDd/6fLmqHxOpPJ2MSIu +te4TxIN+52gnDQJebXYOipcBow1JvVPXyH0KCaP0Fkcgd2NGZPMHJ+ApeHNAHQH3TTv yWbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726902570; x=1727507370; 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=+ohAzvebGtlWdpIGNEivr1Zt3bErXvsBaqlk21HjfVY=; b=gtAvLK/xx3WDrM+UbB2PVfZnbvaxrAzQ/ubu1DDwjcnP8Y4zbisAs6KJoDnZjraHU4 l/Z2czkU9QrT53vEtEi4tXNTanjDXlAWKN0J1tQOAjmJXK3MX6QZaGcUXURXn4Up0Zw/ 0xcvege1IMvNFfkaykdACykrMuGaK0Vht0gzyFZQ6hK1YaEzIqW/0Rj81fm27hvSWPIF MWtasnHaBVOAfnkAUP6/WXEfJpO+GfykgwYl/Vf5FiXOzmY+nScoYYw3kJYKiUQVHQR3 jkA4lwYTMgBNYGZlckXNlJHQ6f0yaAdFNj49qQVyIVzz6z/Icoa3/6+RhlyPl6OMzDYi uaDA== X-Forwarded-Encrypted: i=1; AJvYcCW/Zzr18A3R3/foxwG8bHkDD2HLvl9l+/R2B+LGJkBeTbdAeuTUfUckQ7dNJs0WMnook95wX4xN1A==@kvack.org X-Gm-Message-State: AOJu0YxDziuk43hB1C32rrs9TuVhrT3A9H2fzfEyRQXsShR6jPIxByzb r2Yz9vNjVqnSdRl/a//nCkrg7IBn9Lm6othVSi0weMJAkOJCHZyR X-Google-Smtp-Source: AGHT+IGwsiVPhNv7avWF4x04Wx0XxQzDDJ/r0u3N/C/vOWSYOIyZiQngXhgni063oll3giK30MdWWw== X-Received: by 2002:a05:651c:199f:b0:2f5:375:c1da with SMTP id 38308e7fff4ca-2f7c3d09a2fmr31608841fa.1.1726902569682; Sat, 21 Sep 2024 00:09:29 -0700 (PDT) Received: from work.. (2.133.25.254.dynamic.telecom.kz. [2.133.25.254]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2f79d485f2csm22082221fa.115.2024.09.21.00.09.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Sep 2024 00:09:28 -0700 (PDT) From: Sabyrzhan Tasbolatov To: andreyknvl@gmail.com Cc: akpm@linux-foundation.org, bp@alien8.de, brauner@kernel.org, dave.hansen@linux.intel.com, dhowells@redhat.com, dvyukov@google.com, glider@google.com, hpa@zytor.com, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mingo@redhat.com, ryabinin.a.a@gmail.com, snovitoll@gmail.com, tglx@linutronix.de, vincenzo.frascino@arm.com, x86@kernel.org Subject: [PATCH v4] mm: x86: instrument __get/__put_kernel_nofault Date: Sat, 21 Sep 2024 12:10:05 +0500 Message-Id: <20240921071005.909660-1-snovitoll@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 00E5420008 X-Stat-Signature: jgua7s3qxpwi9yjdcxojcpp455tkyy53 X-Rspam-User: X-HE-Tag: 1726902571-658132 X-HE-Meta: U2FsdGVkX1+GuS4MEpGIXEsozA4yWtRqZ7Tenl5a9TThM8STdX2DTtL5EEIQ7JL8XEDiE4BpDCkpLnUkS8pgMMbm2cypmaECkzFdAojfq7XznBTW4NxHWsmsnRTsreX9SG468LGPTGbf/1dI8oRS7JA7XspYSK36j3N6N//cskyyjIzvmv+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1Qlpd7dLINd7WxMBCaWIibUXbLJZ3zxizIInS/VRfCnmZHAm2nfbrvzBVd4vq1kkju8v1iOZirl6/ug/e4cdmHR5Uj2xWtCoySn2FO4e1wvBmuDTL92Szq7dNAx5wKh8z0vyC+xCI4ReCoBz8nbJOB+/gQlWX/9sKgqvgffpgE9tk91AGUyeL/UV1lInVDIesn8R7PJv55bJGZ0rumx4xhfTm3T96OXgLt0YRrUQ8NEVhiD8QajfNNAQ/Yf4o43sofcA= 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: Instrument copy_from_kernel_nofault(), copy_to_kernel_nofault(), strncpy_from_kernel_nofault() where __put_kernel_nofault, __get_kernel_nofault macros are used. __get_kernel_nofault needs instrument_memcpy_before() which handles KASAN, KCSAN checks for src, dst address, whereas for __put_kernel_nofault macro, instrument_write() check should be enough as it's validated via kmsan_copy_to_user() in instrument_put_user(). __get_user_size was appended with instrument_get_user() for KMSAN check in commit 888f84a6da4d("x86: asm: instrument usercopy in get_user() and put_user()") but only for CONFIG_CC_HAS_ASM_GOTO_OUTPUT. copy_from_to_kernel_nofault_oob() kunit test triggers 4 KASAN OOB bug reports as expected, one for each copy_from/to_kernel_nofault call. Reported-by: Andrey Konovalov Closes: https://bugzilla.kernel.org/show_bug.cgi?id=210505 Signed-off-by: Sabyrzhan Tasbolatov --- v3: changed kunit test from UAF to OOB case and git commit message. v4: updated a grammar in git commit message. --- arch/x86/include/asm/uaccess.h | 4 ++++ mm/kasan/kasan_test.c | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h index 3a7755c1a441..87fb59071e8c 100644 --- a/arch/x86/include/asm/uaccess.h +++ b/arch/x86/include/asm/uaccess.h @@ -353,6 +353,7 @@ do { \ default: \ (x) = __get_user_bad(); \ } \ + instrument_get_user(x); \ } while (0) #define __get_user_asm(x, addr, err, itype) \ @@ -620,6 +621,7 @@ do { \ #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT #define __get_kernel_nofault(dst, src, type, err_label) \ + instrument_memcpy_before(dst, src, sizeof(type)); \ __get_user_size(*((type *)(dst)), (__force type __user *)(src), \ sizeof(type), err_label) #else // !CONFIG_CC_HAS_ASM_GOTO_OUTPUT @@ -627,6 +629,7 @@ do { \ do { \ int __kr_err; \ \ + instrument_memcpy_before(dst, src, sizeof(type)); \ __get_user_size(*((type *)(dst)), (__force type __user *)(src), \ sizeof(type), __kr_err); \ if (unlikely(__kr_err)) \ @@ -635,6 +638,7 @@ do { \ #endif // CONFIG_CC_HAS_ASM_GOTO_OUTPUT #define __put_kernel_nofault(dst, src, type, err_label) \ + instrument_write(dst, sizeof(type)); \ __put_user_size(*((type *)(src)), (__force type __user *)(dst), \ sizeof(type), err_label) diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c index 7b32be2a3cf0..d13f1a514750 100644 --- a/mm/kasan/kasan_test.c +++ b/mm/kasan/kasan_test.c @@ -1899,6 +1899,26 @@ 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); + + ptr = kmalloc(size - KASAN_GRANULE_SIZE, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + + 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), @@ -1971,6 +1991,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), {} };