From patchwork Wed Sep 18 10:56:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sabyrzhan Tasbolatov X-Patchwork-Id: 13806800 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 C95B8CCD19A for ; Wed, 18 Sep 2024 10:57:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 453966B0082; Wed, 18 Sep 2024 06:57:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4020C6B0083; Wed, 18 Sep 2024 06:57:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2C9106B0085; Wed, 18 Sep 2024 06:57:08 -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 0F7686B0082 for ; Wed, 18 Sep 2024 06:57:08 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A7B1BC0500 for ; Wed, 18 Sep 2024 10:57:07 +0000 (UTC) X-FDA: 82577556894.20.813787A Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.47]) by imf24.hostedemail.com (Postfix) with ESMTP id B6B3E180007 for ; Wed, 18 Sep 2024 10:57:05 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Wt+s67/4"; spf=pass (imf24.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.167.47 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=1726656913; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jIjQBepkmzGZvnIkQy1K5zVrOzJD//KUcFSCnlgygqs=; b=QgSl7Tovv+8ixJatGlqS6f5kd5jxMWNvcoevosMtv03Oq5CyXjEMHCIdxJiwmMipf8xuYw byY5vwyHBuO/3Bww9PfuQNJBRDdWItv6IaVnwJs8/Mlv0UBjrMVaonzGhtJQO0lZHRTBr5 CuWBkKBiHheAGUJk8Wz73jnwhGk4hrI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726656913; a=rsa-sha256; cv=none; b=NtmPifIiaq3sHmVKO2UkRQlH+dpVBpfFCMdBUTRp2KUH88JL3wQdPzI5N0OwuIZaSA18Ae Y3ggxhuqQLp1VMSqWcq6ghFZ4jCKfuyTLR3ow3/V+8ZFOilgfIxs2p5UnOzYe1RvFmSb0X SchDgKwB3dnXFthWbSHTH65oRZyD2xs= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Wt+s67/4"; spf=pass (imf24.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.167.47 as permitted sender) smtp.mailfrom=snovitoll@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-5365b6bd901so7429338e87.2 for ; Wed, 18 Sep 2024 03:57:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726657024; x=1727261824; 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=jIjQBepkmzGZvnIkQy1K5zVrOzJD//KUcFSCnlgygqs=; b=Wt+s67/4+1U1RTHm9d4xVzIVPZy6qTB7qbKOBhiIFUFY02NnYGCldw5a4Mc9L6pYmi rstA+29vmXfNX9uWQi4gv2iK0GkBg99nTOerYqTjJQivhMAl1hqZ1Cdei9CzJo3JMqyo m39Dzs/4gYLAPSf+PK9z2rNLnCmbv6x+0nROZecP6yl4zEp1uF9I1cKlsgSARFyjSSzI SEg7P5dtpvYhAUaIILp8BLwizPgGQfPLnRQ0hET+nGriDhER5/pgJnbwT71ROZQ3ghme 5ep8kQt+ojmOc62UkdryeELuR5LYSnwFvXFvppR+JP4WcOrW0FvxQfoWti8YBIP4RCuF /qog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726657024; x=1727261824; 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=jIjQBepkmzGZvnIkQy1K5zVrOzJD//KUcFSCnlgygqs=; b=s3NnUWyByBAxkmOU47baxTdK2SKnUFsPCEJAN5Fy5W7KKIvPCFGlAAJZVQSEtWIYCh SPBm0127MznRJu27dtuY5hu9zY8sUn4JmLZ00vIQTkWHtBh5Eyr7ptHN9Zl17YU33+VX ZTxGtBUpui0CHhrwRzqSimjCyC1jeRWaiUmdGC66nLWFEUYCEKOHeLizEODgBDCm4mjl fpc8H2Z84Lb1fvRwJCKD5x5VrXEw9k9SPFbcXMWO4ZSXpfs6ZhoaSG1xEdbUE11f8m6F mKWvMCUFDMjIbSh0VTGCpWlgTNEZW0cTGcQmyMeD1Mrnu+Clf/thpGjeWE0Zl0b0AUNd 9OJg== X-Forwarded-Encrypted: i=1; AJvYcCVTFPftfULZhZWSKJ3qq1CU2BDectnnxapi/XMxU5rV5G+SD9NI9Gnj3ywfAGr7Wkyz+BZ0SMCPlg==@kvack.org X-Gm-Message-State: AOJu0YwdcesLhE1EAfo7gQ+jg9jpJ0nW6Z9NtwD+P0en+Dc9rO5113S9 tzu72wtDtrmvuB/4kev6DaVw9O41OSSe8uL2KSu4hGlwB6WTdgXl X-Google-Smtp-Source: AGHT+IFqz7ENiL4wqkVZ3XAOWG7V3zHOrWYfWGz0gDIRzRURqETvAt5X2vKD5/78LXvNLLBNtfDRBQ== X-Received: by 2002:a05:6512:ea2:b0:535:6ba7:7725 with SMTP id 2adb3069b0e04-53678fb1cd4mr12757113e87.3.1726657023314; Wed, 18 Sep 2024 03:57:03 -0700 (PDT) Received: from work.. (2.133.25.254.dynamic.telecom.kz. [2.133.25.254]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-536870b8d6fsm1472524e87.291.2024.09.18.03.57.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Sep 2024 03:57:02 -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 v2] mm: x86: instrument __get/__put_kernel_nofault Date: Wed, 18 Sep 2024 15:56:41 +0500 Message-Id: <20240918105641.704070-1-snovitoll@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: 68xqxsyfswboswykezqburea4cw9hk18 X-Rspamd-Queue-Id: B6B3E180007 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1726657025-774156 X-HE-Meta: U2FsdGVkX1/UV5vgeIbUMFayHFDm+hkxuelCPiON0G8XbzAF5ISkuUYos/DrB4vq6BULS11za3P+cLtViyLcVDD1ugdxz5xH7eILjbIexDOwh1rWoTKMlqMW1U2U0kmXO0v/ZCtjoJ0c42VnB+gofVuSriU5TRleoV9NMXQ3sVh7o9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FA8fvh/+c7km+C8rg45YFShCnHT+8apnal9zJi+gS+d4dkltIurXMhVf3/7ajvNunVVemzUr3clK7zS1dDhxCrIrnRPeeLhrUvAM4fupRQlLNm+j4WpxH810AQFvhuNRxaj6mDg38KULppITbwhg+s0Yi+hE7YkJgDpYx1/olgoMRyG7fnk1qvjTWQGSqOes1yX57LfSwyUN9aI7q2OfB6MBGYwV1W52kXi9FD4SiNFSzI0CjNG3dt2cJEo4euOSynSwaUQDAX9wX3R6KdBmajLaYyZxYyBc0RQYP5lSqvz35qu 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() kunit test triggers 4 KASAN bug reports as expected per 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 --- v2: added 2 tests for src, dst check and enhanced instrument check in macros On Wed, Sep 18, 2024 at 3:51 AM Andrey Konovalov wrote: > > On Tue, Sep 17, 2024 at 10:18 PM Sabyrzhan Tasbolatov > wrote: > > > I think the easiest fix would be to allocate e.g. 128 - > KASAN_GRANULE_SIZE bytes and do an out-of-bounds up to 128 bytes via > copy_to/from_kernel_nofault. This will only corrupt the in-object > kmalloc redzone, which is not harmful. Hi Andrey, Thanks for the comments. I've changed the target UAF buffer size to KASAN_GRANULE_SIZE and added other test cases from bugzilla. --- 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..bd1ee79238a2 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..9a3c4ad91d59 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(struct kunit *test) +{ + char *ptr; + char buf[KASAN_GRANULE_SIZE]; + size_t size = sizeof(buf); + + ptr = kmalloc(size, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + kfree(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)); +} + 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), {} };