From mboxrd@z Thu Jan 1 00:00:00 1970 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 C4DF7EB64D8 for ; Wed, 14 Jun 2023 15:52:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C6F96B0074; Wed, 14 Jun 2023 11:52:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 477796B0075; Wed, 14 Jun 2023 11:52:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 366C16B0078; Wed, 14 Jun 2023 11:52:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 28BD96B0074 for ; Wed, 14 Jun 2023 11:52:53 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E872B12040F for ; Wed, 14 Jun 2023 15:52:52 +0000 (UTC) X-FDA: 80901796584.07.798E5D3 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf23.hostedemail.com (Postfix) with ESMTP id 2F90E14000C for ; Wed, 14 Jun 2023 15:52:50 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=YzdjsJvW; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf23.hostedemail.com: domain of lee@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=lee@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686757970; 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=y6LPChQSRhhiibiXO5gI1F80X7UM4q9bVS3dK9J5VyI=; b=7j8FlwxZd/8DB4HWRXdpqiYeF+nBpEe0MJ+DBUfQC4i7wOT4U9Ab8663nBRG2vgIgIPPWZ XzKquaaNeDpbCvEBW44iZEsu335lSh4gTc+/IoFyLowp7tXodGavf43BxwW6K7HdPUUdq4 j0bqYrxVeNGStWXWyPErA9iFzGzDtrs= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=YzdjsJvW; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf23.hostedemail.com: domain of lee@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=lee@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686757970; a=rsa-sha256; cv=none; b=sa8mLcxTOxaGb7Vk0SMv56tQHn+/XQpA1JLjT88FyyjGzUr7cCTOtbkRN8UaLISLGKuUja 8TbNDZtJgrAtZeYWripp81R2KEFTxHnoWxw09Tr1LqOFSfmqf9MyDrUCgIvuNHChHm2wx+ ayQFkyV8KOuFkJyZg20USkkVzq+CCZw= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 50A1E642F1; Wed, 14 Jun 2023 15:52:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 49C99C433C8; Wed, 14 Jun 2023 15:52:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686757968; bh=AJyVucwoIxaM7hk3ir8inG10Nh04x4lV/Oh6Cfm53pE=; h=From:To:Cc:Subject:Date:From; b=YzdjsJvWQr6h2yhmVELon5Fqk487hQhdR8rpMkV52G9CttvAFNz+K9pVfp1LDXyyz DwcUhPrUoIv0EEPxP795WtniC1fvaB+16D9y5hobtmo1QdzH+q5xQ7NFzEMta9yDD3 70N6lLinPTMw0rmuoKtdj0UGWE3xNmqM2RpiEqBQGx8Uopa5r044GvyusGHU05AW8t tmoUnO61B9YoWQZK7507sTEXyy+P1RT2EnmiiApHxtXdbXX2m0s3DrMEC8VVDItCMj 7T84a4KSDwyzrVt4SqH+YKKuaLJsMy1J8oPyWN4s4qKZc/wuyYrWnG7STHNSrpJjRL dcrXh3BIlMwDg== From: Lee Jones To: lee@kernel.org, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH 0/1] x86: Fix .bss corruption Date: Wed, 14 Jun 2023 16:52:09 +0100 Message-ID: <20230614155223.905222-1-lee@kernel.org> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 2F90E14000C X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 5n5yikb3mg8ednneosrb6n9bbsnmdf93 X-HE-Tag: 1686757970-904114 X-HE-Meta: U2FsdGVkX1+1ECk+YE9jgB23BCZRIwEhx2wnCxjp8cwSF59EcufIxJwCGcHd7oaG7L6/0fP0anLqTQlTzA/nbuWh1etoxpciR2tK6KHmz9uD9/hK/GrERUbssgXTcwnriUI5e5hqf4fLdoAJfkGnhZJX3ol/ZKUghtBA+FKmYWkf6N/+tyDt8RWNylwBcRKBDEcBiYNEPXKlIZ8cF3SMZRgGfP9S6kAmwa9pPABOqCcu9Ud4Y48AQeEWxGITOL+fUl8iU+lzyaJQ9GPHV1R1ya8RqBURpL6med1NmFVePyXXgNOwoH+P3YOLC6ABZrw3LctOmodO3YSDXMjUV+kjh6q8ji06D3HDdGMHX+ntMVLqeGaaslxQMIJZKGJ0XlHRMfuJTws4a2L8rnc+vUQ2txC0mjAgP9w8tDIW3WuPaFtCTQ0nPGeTgidPrizGbbJDRkdefeTyTrHxGbTrlQAiVDnZqXMj94Cg7pT96ARtxqSxJVcptavk4uOkSjjXzB73h8WtWOfoP1IjT38MHhg614kMmWMYdDkxyzMlEHfQ9IQrZhh2yYmnxgmR+OWZv7FkPQGc/G3rF+DYWaYrTP0vtJJaRWKeOtkwkNfEJ6wPO+Kr2YSRdSQwJr+Mf+PbtsY+K4IUjP39s5ZHdOXs92myztqPX/LKOmNWTC/OaCS+2uDtCUWpRFAZBT4q0xXCTK1SMAQZYTiSwue1xfif9DyIQcqnwloixvLb9HGvsizQfPHjFMOPb/v8yGG8rRau+km/D6cP7v03jHJwAaE6TMTZFAaDEcSpC2cb0ouVzmX00AxkJ25r0AGCQ/434bKH9q5nJFdDoRvnWRW5Svpqj/aUiXurFbX7RmQ1vNtRSFABDlJjQpRxqxr+ovpHqsxUTTlnfSyamDfhfDQz0g5BSMOI0btykGR8SBV3QLkUruAAdRhBwOjt/S+CDNrztiYyJC+5SxCRfTuvZW1UkYwaPds EM5fdVRI XFHd3JMN6YyTswYeMn5WqxB3z8oUOXch9Cp/yRaVsvd/L2DjT7UC/70Y9mmBo+vWb/6zmuyxroMdialb8unrr+i0uOehKs71zTJNckMUa6yAr6jqxMaYcMh/7wPprgAbGt8LsOpjmqeR5XOhY73+8FhaSRjNHYn7252TQ14+89CXDxai4JPXC7KoibUATq/Axgpq6vkEVn501QCemm3bP7/s1MLNNps9cXwWH9z144fplWCjT5iglbENqxDF0NmukWz+OrvyOzy+pFTYVN/ZMqh5ux+qq8do5cNYVFnFpE/AaO6aVQ69DkzbZBEcX9ZG8zgspUuevNImqkWJnkrKLFJpffg== 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: - Report The following highly indeterministic kernel panic was reported to occur every few hundred boots: Kernel panic - not syncing: Fatal exception RIP: 0010:alloc_ucounts+0x68/0x280 RSP: 0018:ffffb53ac13dfe98 EFLAGS: 00010006 RAX: 0000000000000000 RBX: 000000015b804063 RCX: ffff9bb60d5aa500 RDX: 0000000000000001 RSI: 00000000000003fb RDI: 0000000000000001 RBP: ffffb53ac13dfec0 R08: 0000000000000000 R09: ffffd53abf600000 R10: ffff9bb60b4bdd80 R11: ffffffffbcde7bb0 R12: ffffffffbf04e710 R13: ffffffffbf405160 R14: 00000000000003fb R15: ffffffffbf1afc60 FS: 00007f5be44d5000(0000) GS:ffff9bb6b9cc0000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 000000015b80407b CR3: 00000001038ea000 CR4: 00000000000406a0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: __sys_setuid+0x1a0/0x290 __x64_sys_setuid+0xc/0x10 do_syscall_64+0x43/0x90 ? asm_exc_page_fault+0x8/0x30 entry_SYSCALL_64_after_hwframe+0x44/0xae - Problem The issue was eventually tracked down to the attempted dereference of a value located in a corrupted hash table. ucounts_hashtable is an array of 1024 struct hlists. Each element is the head of its own linked list where previous ucount allocations are stored. The [20]th element of ucounts_hashtable was being consistently trashed on each and every boot. However the indeterminism comes from it being accessed only every few hundred boots. The issue disappeared, or was at least unidentifiable when !(LTO=full) or when memory base randomisation (a.k.a. KASLR) was disabled, rending GDB all but impossible to use effectively. The cause of the corruption was uncovered using a verity of different debugging techniques and was eventually tracked down to page table manipulation in early architecture setup. The following line in arch/x86/realmode/init.c [0] allocates a variable, just 8 Bytes in size, to "hold the pgd entry used on booting additional CPUs": pgd_t trampoline_pgd_entry; The address of that variable is then passed from init_trampoline_kaslr() via a call to set_pgd() [1] to have a value (not too important here) assigned to it. Numerous abstractions take place, eventually leading to native_set_p4d(), an inline function [2] contained in arch/x86/include/asm/pgtable_64.h. >From here, intentionally or otherwise, a call to pti_set_user_pgtbl() is made. This is where the out-of-bounds write eventually occurs. It is not known (by me) why this function is called. The returned result is subsequently used as a value to write using the WRITE_ONCE macro. Perhaps the premature write is not intended. This is what I hope to find out. A little way down in pti_set_user_pgtbl() [3] the following line occurs: kernel_to_user_pgdp(pgdp)->pgd = pgd.pgd The kernel_to_user_pgdp() part takes the address of pgdp (a.k.a. trampoline_pgd_entry) and ends up flipping the 12th bit, essentially adding 4k (0x1000) to the address. Then the part at the end assigns our value (still not important here) to it. However, if we remember that only 8 Bytes was allocated (globally) for trampoline_pgd_entry, then means we just stored the value into the outlands (what we now know to be allocated to another global storage user ucounts_hashtable). [0] https://elixir.bootlin.com/linux/latest/source/arch/x86/realmode/init.c#L18 [1] https://elixir.bootlin.com/linux/latest/source/arch/x86/mm/kaslr.c#L178 [2] https://elixir.bootlin.com/linux/latest/source/arch/x86/include/asm/pgtable_64.h#L142 [3] https://elixir.bootlin.com/linux/latest/source/arch/x86/mm/pti.c#L142 Lee Jones (1): x86/mm/KASLR: Store pud_page_tramp into entry rather than page arch/x86/mm/kaslr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) -- 2.41.0.162.gfafddb0af9-goog