Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6461013C67C for ; Tue, 26 Mar 2024 15:17:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711466252; cv=none; b=PggoWlDeTmOgckFnlx3t8iyAjhVOHVC60aTgVsGsE1s0Uu8j5CH0NbryHgQSpIaHZikz8+iaB4fzTlhwyRhFvLSb8RZ9npJFryCNKLfLAS471FeXZZe8+/uXi3SMUGtZk8ic5UxQufAO2pK0kUuzN3YoV2GWRb0lNtiCxsq/54o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711466252; c=relaxed/simple; bh=a+3dnnizi/bZaanOrjbnycqrAbz2RXZy1SbtMMMTqXY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=T5yM0tM1MlNB9alyrpQPVv7vn8W2BcQODxa3LngY9DjtCYMaWjZssc/eT7WO7FoIZbp+hD3pPPAhLw3l6B1n/VRTGuZS2TXiklvuWhfolRSgjTMiooJ6//kXv1Z9bYCIAwpNct+D41XX1wDvI0Na6lB98+e8OAYd0W2UBAG4q8w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CPY/Tptq; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CPY/Tptq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 42DF4C433F1; Tue, 26 Mar 2024 15:17:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711466250; bh=a+3dnnizi/bZaanOrjbnycqrAbz2RXZy1SbtMMMTqXY=; h=From:To:Cc:Subject:Date:Reply-to:From; b=CPY/TptqqiI1/UGa02V+7mSqR9gEEhof0vnq2oUJ0KfZyB3kM5SLYBxqCnLjzhaXj 695I61fTQa4O7YLZxcmcJMvOGEDmgkpY2jj0Jwi+OKkgxpx7sv8SWSwQ9emBdB4HSu EPWv+xqdjllIKfbhJd5km107NEazw24M4IAT2n+fv9lRO/dFWwZIWlq9AjLOhjoLai AKAUJ/F7R5KywZa8VbOwZMg/Bl8b+JBZEcIBPjKJsDfYwJ0ky+dvBEOwjb60mXNejq RW6FlzziGWOfRfIq1m6pG9/DPOquEwbj83KjDiRj4H5oQyeRNcPIyJ61s+jCThtdPH CTQd9ELYm+NoQ== From: Lee Jones To: linux-cve-announce@vger.kernel.org Cc: Lee Jones Subject: CVE-2024-26645: tracing: Ensure visibility when inserting an element into tracing_map Date: Tue, 26 Mar 2024 15:17:24 +0000 Message-ID: <20240326151722.1258576-4-lee@kernel.org> X-Mailer: git-send-email 2.44.0.396.g6e790dbe36-goog Precedence: bulk X-Mailing-List: linux-cve-announce@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Reply-to: , X-Developer-Signature: v=1; a=openpgp-sha256; l=7520; i=lee@kernel.org; h=from:subject; bh=a+3dnnizi/bZaanOrjbnycqrAbz2RXZy1SbtMMMTqXY=; b=owEBbQKS/ZANAwAKAVGvii+H/HdhAcsmYgBmAucDcGzne8u2bXj2ZfeB6e9GO8vL+11EZXFi2 8y0zI7byTGJAjMEAAEKAB0WIQR2tsk1o74gmpTwh0hRr4ovh/x3YQUCZgLnAwAKCRBRr4ovh/x3 YdBaEACEt5i4xuC4gFiWtgi/H9nbG0vtD6jMEGONpqzsdHOEvLaXLzAzgSCveNmBGVGyn3mlBka xEA7FVXC7yFbbT6jmCzPMLEOw+t9PbTBw2bndkAFOkMENmWFkl43N/x9zO6FBkIF2GsjN33V+ky 7Cgss2mKW/94bxcmSvhJDv1d3gkzB+OPNEfBWLT9LBnAUs+nTgKvKA44IXYFhTzwD96b6FvKRMg 5bWkuzClLSSUaNw2TxuDBtkj0IQu1pHhUVQ448og7c9wPrCp2LiMYDtsNH+HayoKlEjyigaNNC0 99GZ7iqNbtJw902yhVXy7qsFUIdhtjo3i8qKvekkIWohHXpZ2vpGE9q5lapYbNW7WaNojk3mH8i 8zUzV85j+z9BJSREsKBxuuj5xsVtC7MPwZxbLWnHsApWmaTZNieT2JcpPDNqrxMP7yjQ3T6gegq 3QBqxmp9b9lq+1NIHRMeTczohENSDIv1hwFCfxQojscbUeUYfvsbriWtzyCCynUwY95tgqwCJqx s5r4XzqudxxKB98+Yld3viZN1Z5lXzrjCU01f1MXAgVFcKP0Q7H9ROiLcrueY4EFBjvW//+BE4n c3uyUsV6cimp0PGH9aVayAxO47X6y6F0EJ/j3tqHAwvxogrJNkStJ/8xAvADn6sMoX+q3w/lXHg m8k0jaO7CyButqA== X-Developer-Key: i=lee@kernel.org; a=openpgp; fpr=76B6C935A3BE209A94F0874851AF8A2F87FC7761 Content-Transfer-Encoding: 8bit Description =========== In the Linux kernel, the following vulnerability has been resolved: tracing: Ensure visibility when inserting an element into tracing_map Running the following two commands in parallel on a multi-processor AArch64 machine can sporadically produce an unexpected warning about duplicate histogram entries: $ while true; do echo hist:key=id.syscall:val=hitcount > \ /sys/kernel/debug/tracing/events/raw_syscalls/sys_enter/trigger cat /sys/kernel/debug/tracing/events/raw_syscalls/sys_enter/hist sleep 0.001 done $ stress-ng --sysbadaddr $(nproc) The warning looks as follows: [ 2911.172474] ------------[ cut here ]------------ [ 2911.173111] Duplicates detected: 1 [ 2911.173574] WARNING: CPU: 2 PID: 12247 at kernel/trace/tracing_map.c:983 tracing_map_sort_entries+0x3e0/0x408 [ 2911.174702] Modules linked in: iscsi_ibft(E) iscsi_boot_sysfs(E) rfkill(E) af_packet(E) nls_iso8859_1(E) nls_cp437(E) vfat(E) fat(E) ena(E) tiny_power_button(E) qemu_fw_cfg(E) button(E) fuse(E) efi_pstore(E) ip_tables(E) x_tables(E) xfs(E) libcrc32c(E) aes_ce_blk(E) aes_ce_cipher(E) crct10dif_ce(E) polyval_ce(E) polyval_generic(E) ghash_ce(E) gf128mul(E) sm4_ce_gcm(E) sm4_ce_ccm(E) sm4_ce(E) sm4_ce_cipher(E) sm4(E) sm3_ce(E) sm3(E) sha3_ce(E) sha512_ce(E) sha512_arm64(E) sha2_ce(E) sha256_arm64(E) nvme(E) sha1_ce(E) nvme_core(E) nvme_auth(E) t10_pi(E) sg(E) scsi_mod(E) scsi_common(E) efivarfs(E) [ 2911.174738] Unloaded tainted modules: cppc_cpufreq(E):1 [ 2911.180985] CPU: 2 PID: 12247 Comm: cat Kdump: loaded Tainted: G E 6.7.0-default #2 1b58bbb22c97e4399dc09f92d309344f69c44a01 [ 2911.182398] Hardware name: Amazon EC2 c7g.8xlarge/, BIOS 1.0 11/1/2018 [ 2911.183208] pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--) [ 2911.184038] pc : tracing_map_sort_entries+0x3e0/0x408 [ 2911.184667] lr : tracing_map_sort_entries+0x3e0/0x408 [ 2911.185310] sp : ffff8000a1513900 [ 2911.185750] x29: ffff8000a1513900 x28: ffff0003f272fe80 x27: 0000000000000001 [ 2911.186600] x26: ffff0003f272fe80 x25: 0000000000000030 x24: 0000000000000008 [ 2911.187458] x23: ffff0003c5788000 x22: ffff0003c16710c8 x21: ffff80008017f180 [ 2911.188310] x20: ffff80008017f000 x19: ffff80008017f180 x18: ffffffffffffffff [ 2911.189160] x17: 0000000000000000 x16: 0000000000000000 x15: ffff8000a15134b8 [ 2911.190015] x14: 0000000000000000 x13: 205d373432323154 x12: 5b5d313131333731 [ 2911.190844] x11: 00000000fffeffff x10: 00000000fffeffff x9 : ffffd1b78274a13c [ 2911.191716] x8 : 000000000017ffe8 x7 : c0000000fffeffff x6 : 000000000057ffa8 [ 2911.192554] x5 : ffff0012f6c24ec0 x4 : 0000000000000000 x3 : ffff2e5b72b5d000 [ 2911.193404] x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0003ff254480 [ 2911.194259] Call trace: [ 2911.194626] tracing_map_sort_entries+0x3e0/0x408 [ 2911.195220] hist_show+0x124/0x800 [ 2911.195692] seq_read_iter+0x1d4/0x4e8 [ 2911.196193] seq_read+0xe8/0x138 [ 2911.196638] vfs_read+0xc8/0x300 [ 2911.197078] ksys_read+0x70/0x108 [ 2911.197534] __arm64_sys_read+0x24/0x38 [ 2911.198046] invoke_syscall+0x78/0x108 [ 2911.198553] el0_svc_common.constprop.0+0xd0/0xf8 [ 2911.199157] do_el0_svc+0x28/0x40 [ 2911.199613] el0_svc+0x40/0x178 [ 2911.200048] el0t_64_sync_handler+0x13c/0x158 [ 2911.200621] el0t_64_sync+0x1a8/0x1b0 [ 2911.201115] ---[ end trace 0000000000000000 ]--- The problem appears to be caused by CPU reordering of writes issued from __tracing_map_insert(). The check for the presence of an element with a given key in this function is: val = READ_ONCE(entry->val); if (val && keys_match(key, val->key, map->key_size)) ... The write of a new entry is: elt = get_free_elt(map); memcpy(elt->key, key, map->key_size); entry->val = elt; The "memcpy(elt->key, key, map->key_size);" and "entry->val = elt;" stores may become visible in the reversed order on another CPU. This second CPU might then incorrectly determine that a new key doesn't match an already present val->key and subsequently insert a new element, resulting in a duplicate. Fix the problem by adding a write barrier between "memcpy(elt->key, key, map->key_size);" and "entry->val = elt;", and for good measure, also use WRITE_ONCE(entry->val, elt) for publishing the element. The sequence pairs with the mentioned "READ_ONCE(entry->val);" and the "val->key" check which has an address dependency. The barrier is placed on a path executed when adding an element for a new key. Subsequent updates targeting the same key remain unaffected. From the user's perspective, the issue was introduced by commit c193707dde77 ("tracing: Remove code which merges duplicates"), which followed commit cbf4100efb8f ("tracing: Add support to detect and avoid duplicates"). The previous code operated differently; it inherently expected potential races which result in duplicates but merged them later when they occurred. The Linux kernel CVE team has assigned CVE-2024-26645 to this issue. Affected and fixed versions =========================== Issue introduced in 4.17 with commit c193707dde77 and fixed in 4.19.307 with commit 5022b331c041 Issue introduced in 4.17 with commit c193707dde77 and fixed in 5.4.269 with commit dad9b28f675e Issue introduced in 4.17 with commit c193707dde77 and fixed in 5.10.210 with commit ef70dfa0b1e5 Issue introduced in 4.17 with commit c193707dde77 and fixed in 5.15.149 with commit aef1cb00856c Issue introduced in 4.17 with commit c193707dde77 and fixed in 6.1.76 with commit f4f7e696db02 Issue introduced in 4.17 with commit c193707dde77 and fixed in 6.6.15 with commit a1eebe76e187 Issue introduced in 4.17 with commit c193707dde77 and fixed in 6.7.3 with commit bf4aeff7da85 Issue introduced in 4.17 with commit c193707dde77 and fixed in 6.8 with commit 2b44760609e9 Please see https://www.kernel.org for a full list of currently supported kernel versions by the kernel community. Unaffected versions might change over time as fixes are backported to older supported kernel versions. The official CVE entry at https://cve.org/CVERecord/?id=CVE-2024-26645 will be updated if fixes are backported, please check that for the most up to date information about this issue. Affected files ============== The file(s) affected by this issue are: kernel/trace/tracing_map.c Mitigation ========== The Linux kernel CVE team recommends that you update to the latest stable kernel version for this, and many other bugfixes. Individual changes are never tested alone, but rather are part of a larger kernel release. Cherry-picking individual commits is not recommended or supported by the Linux kernel community at all. If however, updating to the latest release is impossible, the individual changes to resolve this issue can be found at these commits: https://git.kernel.org/stable/c/5022b331c041e8c54b9a6a3251579bd1e8c0fc0b https://git.kernel.org/stable/c/dad9b28f675ed99b4dec261db2a397efeb80b74c https://git.kernel.org/stable/c/ef70dfa0b1e5084f32635156c9a5c795352ad860 https://git.kernel.org/stable/c/aef1cb00856ccfd614467cfb50b791278992e177 https://git.kernel.org/stable/c/f4f7e696db0274ff560482cc52eddbf0551d4b7a https://git.kernel.org/stable/c/a1eebe76e187dbe11ca299f8dbb6e45d5b1889e7 https://git.kernel.org/stable/c/bf4aeff7da85c3becd39fb73bac94122331c30fb https://git.kernel.org/stable/c/2b44760609e9eaafc9d234a6883d042fc21132a7