From patchwork Thu Sep 19 10:57:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sabyrzhan Tasbolatov X-Patchwork-Id: 13807634 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 CC5CFCE8D78 for ; Thu, 19 Sep 2024 10:57:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 52BAF6B0098; Thu, 19 Sep 2024 06:57:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4DB5E6B009A; Thu, 19 Sep 2024 06:57:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 37BFB6B009B; Thu, 19 Sep 2024 06:57:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1A0B56B0098 for ; Thu, 19 Sep 2024 06:57:27 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D3244AB0D9 for ; Thu, 19 Sep 2024 10:57:26 +0000 (UTC) X-FDA: 82581186492.03.BB61315 Received: from mail-lj1-f179.google.com (mail-lj1-f179.google.com [209.85.208.179]) by imf05.hostedemail.com (Postfix) with ESMTP id DDE26100005 for ; Thu, 19 Sep 2024 10:57:24 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="CnpP5Z/P"; spf=pass (imf05.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.208.179 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=1726743388; 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=mJ9HZHJD0QLNFkZHIpeJlyi0Ek76HYhPtHMe5HLX1eo=; b=Y7QGwxn5rzF3DUWrm6sAclJLR13PCh6IluITV3wjM454RjHsUFyJbCUPrh0F95QuB1dV3T urGQLSZdSJmX9VYZLIy95292IVxM3GeFXkHALFoJRhn64aNwvvAqaBHpXud1hlJLQx+1m4 qLXhLDuWiwr49Lvr54Yw8SixjqlS1kk= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="CnpP5Z/P"; spf=pass (imf05.hostedemail.com: domain of snovitoll@gmail.com designates 209.85.208.179 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=1726743388; a=rsa-sha256; cv=none; b=22S77/xoAXwWKfGP8LH+hBEg3cdtXIQtwW4SKRXzRclNVVtyKQAEURB45JkUwF20jeZgp8 SlqqqKA/qCZEqGu17ZHDEOZycjnX0el61uBba23WfwswXLQmfvt+0qRaBDEcMR4pai6mbj yVr5iuY/N0zZ9heqsA9LAcdz813btK0= Received: by mail-lj1-f179.google.com with SMTP id 38308e7fff4ca-2f78b28ddb6so6593471fa.0 for ; Thu, 19 Sep 2024 03:57:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726743443; x=1727348243; 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=mJ9HZHJD0QLNFkZHIpeJlyi0Ek76HYhPtHMe5HLX1eo=; b=CnpP5Z/P2lHuWAmTuMJSIAdB+QwIWKwbjBgMr+3Mi51Dx7Kn/D9yBfZrGkYctXPAqZ VWpGNHJbvHdSCERV2ASN9ug9piXsz1epHDtKUBnO27x/HBFXym2SAl4oiGcJa23+63ji Dv14z8r2+m7pXjUaZfFca00pQUbrZhtkxW31F/aUg1RjQ0qv0XRHehVepGDqRneOHIcP m/ohg1OzfrWoXPtsmMzKDre57RTEBLoL/2uUP8bR0HA5lYX8NHgIygELc/qEwKXOBP0j qQnCLN+nHejcGGxMpJNNshIJbAVP2AWswiKoOtPuBznoQTV21hCyqROBisy3o86plDtb wyTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726743443; x=1727348243; 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=mJ9HZHJD0QLNFkZHIpeJlyi0Ek76HYhPtHMe5HLX1eo=; b=p/CSAKJZxQviU5x8czekXkf6Hqx6c1zLEz3NGPI+4QYHcCtytMygynz9w7cdZVA5Si 3h2TPOamZIu+CCRTkfNCSDsrC99rZhiAhoeDwaHjCO5Fv3PfqvoBXup+ocm1zAsi6aMI cq0QH9gmngJLtMfzmoEDc5xcpA1YQlkOoE5EEzp2Zja/D6185CH5QyrfmLIv2qLGbInq MDUwa+uKTvCUcXxjyVIDd9mWahsPryb/p3c54X2msz4tcBlesyjHFRc6NM3cFZLc7Clj v9VS1ZxiuPEvl5FbTrZzI5WyCAk9r1j+fd6aF02zyNqmMo5guSyhE66ZLL2xH9jyFjXi AUCA== X-Forwarded-Encrypted: i=1; AJvYcCUtFibJVtsuoE9XtBTWfiv7dX1K9G29kS23pWZTWvyKbcpsBaZBOuJnY9R8d+1KW3LwytGfwL7puA==@kvack.org X-Gm-Message-State: AOJu0YwNRE57z58k+HQw2u4NkfD3WXRZ+kqm9InldJKxDbdnmpUFoSrf kidntmHodaVMqEFJx28VWDs3jddlRfv6q/dqTKwaIhzy//imkilA X-Google-Smtp-Source: AGHT+IFCefsOZr/jmfhZfRtwZLYLHEqhbkYUuHJUtLH/v8E7pnynNcbVdgR2uDruY5y8SnFBAzqllA== X-Received: by 2002:a05:651c:154a:b0:2f6:4aed:9973 with SMTP id 38308e7fff4ca-2f787f5833dmr156325931fa.44.1726743442445; Thu, 19 Sep 2024 03:57:22 -0700 (PDT) Received: from work.. (2.133.25.254.dynamic.telecom.kz. [2.133.25.254]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2f79d2e1d8csm16223341fa.21.2024.09.19.03.57.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Sep 2024 03:57:21 -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 v3] mm: x86: instrument __get/__put_kernel_nofault Date: Thu, 19 Sep 2024 15:57:50 +0500 Message-Id: <20240919105750.901303-1-snovitoll@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: DDE26100005 X-Stat-Signature: kkzgk3gfff6tkukxw6uyxrr6eefbny5z X-HE-Tag: 1726743444-43466 X-HE-Meta: U2FsdGVkX18gzmFeHzJFnhE/POW9wxM058FLcJPE7kk7LKDKleggcHJU4vflHIq6K0Z1Xw2V3GlvPw9tYQreynkZITmuwrlMYLmtwSt5jeGS+vOlZyMspV7GU9Dy9biks+477CxgkK+wPCyA8l1W4J1bdwSoUVSeeXTgNdW9DEKohET0UFJgSCgsb60dshMb2Thfuprw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ocgT/1WQ0Edr3lgCoPZyw783mMcaAOGeqCFeseQKmOtmER3ZxF1BrMbfQsFtKMkq+7TTiuLzlucqXugLcViA1zYMk/KZZ2c4N0mnXLImWjK3nXBkv5p69EDMRLxLJAvrGlKFFj6rupCQQ1wgu6cYQmJ8mpVHXQyLA9GxTCLaW8I1tfI4ouAsE7hxi2mt3mqyiCeJZSmN5k/yhOwRYalrzebCHLcP/mSH25lYsZ7/eYJ0wduL9YpzTP+f1TfzzFeWwZs9Sd4kqPmUdLE5/3IRcq2ba6Zxp7kNw== 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: On Wed, Sep 18, 2024 at 8:15 PM Andrey Konovalov wrote: > You still have the same problem here. > > What I meant is: > > char *ptr; > char buf[128 - KASAN_GRANULE_SIZE]; > size_t size = sizeof(buf); > > ptr = kmalloc(size, GFP_KERNEL); > KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); > > KUNIT_EXPECT_KASAN_FAIL(...); > ... > > kfree(ptr); Thanks for catching this! I've turned kunit test into OOB instead of UAF. --- v3: changed kunit test from UAF to OOB case and git commit message. --- 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 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 --- 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), {} };