From patchwork Fri Oct 25 13:31:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Koichiro Den X-Patchwork-Id: 2002238 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XZkGc3tpjz1xwy for ; Sat, 26 Oct 2024 00:32:08 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1t4KQ2-0006wk-VN; Fri, 25 Oct 2024 13:31:58 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1t4KQ1-0006wU-Gv for kernel-team@lists.ubuntu.com; Fri, 25 Oct 2024 13:31:57 +0000 Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id B9AB43FA4D for ; Fri, 25 Oct 2024 13:31:56 +0000 (UTC) Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-7203cdc239dso2279217b3a.3 for ; Fri, 25 Oct 2024 06:31:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729863115; x=1730467915; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zbGfZP4phz53BceaoCr8+HbKnUDQPXQgMeP2WriryHU=; b=dhBsGTCVJIjJ/nWOMLG7xzrOgXT1Wftozf2+hiSLRB9fSTFHScYLqit77jjjtGK+uj GP9TS6G89OAszb3ZRWxr4tOn7Q2DiVdmFh9Z0Menh0lPxZ+95lPl3bZzWsD3I/mJe+ey XXYLJk6JJxut3WbMofvRaW7qLsdILQvbk9oANy7FkAnwSo9/19Riil327ueh+aW8mqQg sJL61FHp9tGGClAidbDkloEtL2yBAHvajXPuBpyPBCxWcg1XqeBvKbt+mA2puRCgsjUV Umh42b6jMAfO3I65IWBlc1Af9em+XpK0eS/D2kqk0gSe4XpwXMB5OsbqAJPJDfqIxaqv mCVw== X-Gm-Message-State: AOJu0YxELwpg5bhJRSjWrj8S4Z7kIFX8JxacOrUz914glAIxyMZtJ9Lq 7hvys43DotGu3511/p75gtGoD/9DuP68IZpy1VLWqL8CTwGEBV1uWAJufz7DxgZQopRR985xAcr 6bI4oCCfYprfPGN+GmKtglDJ5If8R/4rs4AT5RiGcd947a/hZlz4dE6+UlpgjbtXiRNen2WNcDL bkXPhoFWBYxw== X-Received: by 2002:a05:6a21:2d8c:b0:1d9:6c07:6481 with SMTP id adf61e73a8af0-1d978bd4b70mr11603339637.42.1729863115060; Fri, 25 Oct 2024 06:31:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHEtqVcoIXlr1spraMbGP0xJcVZCC57jdxBTcI7fygRKEExxynyE2zaJBtJPtKFTAn0fRfFcQ== X-Received: by 2002:a05:6a21:2d8c:b0:1d9:6c07:6481 with SMTP id adf61e73a8af0-1d978bd4b70mr11603311637.42.1729863114708; Fri, 25 Oct 2024 06:31:54 -0700 (PDT) Received: from localhost.localdomain ([240f:74:7be:1:9b52:57a4:8bdb:99d3]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921983sm1077883b3a.10.2024.10.25.06.31.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2024 06:31:54 -0700 (PDT) From: Koichiro Den To: kernel-team@lists.ubuntu.com Subject: [SRU][F][PATCH 1/3] ftrace: Separate out functionality from ftrace_location_range() Date: Fri, 25 Oct 2024 22:31:04 +0900 Message-ID: <20241025133110.2839066-2-koichiro.den@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241025133110.2839066-1-koichiro.den@canonical.com> References: <20241025133110.2839066-1-koichiro.den@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: "Steven Rostedt (VMware)" Create a new function called lookup_rec() from the functionality of ftrace_location_range(). The difference between lookup_rec() is that it returns the record that it finds, where as ftrace_location_range() returns only if it found a match or not. The lookup_rec() is static, and can be used for new functionality where ftrace needs to find a record of a specific address. Signed-off-by: Steven Rostedt (VMware) (backported from commit 7e16f581a81759bafea04d049134b32d1a881226) [koichiroden: Prerequisite for "ftrace: Fix possible use-after-free issue in ftrace_location()". Adjusted context due to an extra commit 170d0fe552d0 in this tree, backported from v5.4.238 ac58b88ccbbb ("ftrace: Fix invalid address access in lookup_rec() when index is 0")] CVE-2024-38588 Signed-off-by: Koichiro Den --- kernel/trace/ftrace.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 412505d94865..d9992fef7287 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -1535,22 +1535,10 @@ static int ftrace_cmp_recs(const void *a, const void *b) return 0; } -/** - * ftrace_location_range - return the first address of a traced location - * if it touches the given ip range - * @start: start of range to search. - * @end: end of range to search (inclusive). @end points to the last byte - * to check. - * - * Returns rec->ip if the related ftrace location is a least partly within - * the given address range. That is, the first address of the instruction - * that is either a NOP or call to the function tracer. It checks the ftrace - * internal tables to determine if the address belongs or not. - */ -unsigned long ftrace_location_range(unsigned long start, unsigned long end) +static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end) { struct ftrace_page *pg; - struct dyn_ftrace *rec; + struct dyn_ftrace *rec = NULL; struct dyn_ftrace key; key.ip = start; @@ -1564,9 +1552,29 @@ unsigned long ftrace_location_range(unsigned long start, unsigned long end) rec = bsearch(&key, pg->records, pg->index, sizeof(struct dyn_ftrace), ftrace_cmp_recs); - if (rec) - return rec->ip; } + return rec; +} + +/** + * ftrace_location_range - return the first address of a traced location + * if it touches the given ip range + * @start: start of range to search. + * @end: end of range to search (inclusive). @end points to the last byte + * to check. + * + * Returns rec->ip if the related ftrace location is a least partly within + * the given address range. That is, the first address of the instruction + * that is either a NOP or call to the function tracer. It checks the ftrace + * internal tables to determine if the address belongs or not. + */ +unsigned long ftrace_location_range(unsigned long start, unsigned long end) +{ + struct dyn_ftrace *rec; + + rec = lookup_rec(start, end); + if (rec) + return rec->ip; return 0; } From patchwork Fri Oct 25 13:31:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Koichiro Den X-Patchwork-Id: 2002239 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XZkGc6S1Sz1xxK for ; Sat, 26 Oct 2024 00:32:08 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1t4KQ5-0006yK-6p; Fri, 25 Oct 2024 13:32:01 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1t4KQ4-0006x3-2Z for kernel-team@lists.ubuntu.com; Fri, 25 Oct 2024 13:32:00 +0000 Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 7213D3FA4D for ; Fri, 25 Oct 2024 13:31:58 +0000 (UTC) Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-71e49ae1172so2132817b3a.2 for ; Fri, 25 Oct 2024 06:31:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729863117; x=1730467917; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kyWYdulo+5Wnop/otlAvZ9hUIt7udwwe5mn2YjrAI3I=; b=St/h8oWxBnoivcBR+fvLPFakcGTTFheXs9MppAxmjPei9yqBeuB8jzBkWfsPYWSztc A2U8SyIi35j8X8euBBEnSYEGLmYRmyHI8S90TB9/dnRb22T8gppsY66CbjB8d+EK+DoJ sfEZvEwWbVlOYOKe9n0XJj59nGC9vseT/jltd3079/MyqxvF3d++jrTeGHpy7tTgS+up nrQiAXUaLUg2zvxJy5x2S9wXY60Da8fd0nVmp1mA0xyucSYbNWx1MtcGh1V2YJSkqO/V pizADI9MczcRGBaGvc98JvXAQjTrA+URXT+w7dc31PJPGUoV+KXRLpe7APpKaKFZdPDG mECw== X-Gm-Message-State: AOJu0YwdOyerW7eTatwGY3rHN4lwdnGid3ImXUURrjROwEhcZnTbbNsS Zlt0JnhlHxmmmdl8EdnMRvtYEKJVzQ4U28BMidPA9EQKBfqdzxniCJEx4U1q0AagrpfuCo5gf60 gpY7tqg0gtM3S40rP445K0tLZlwHw9eIySW0piqU6Jrd9B5DyVjE4O6PQd41YjwiwS6oGML6qAf CyRV+NX4TRhg== X-Received: by 2002:a05:6a20:d04e:b0:1d5:1354:5256 with SMTP id adf61e73a8af0-1d989cf91c2mr6453403637.39.1729863116598; Fri, 25 Oct 2024 06:31:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFz+XZa+lnVncUAAAAanbCUxZQo3gkFh173XEckb14mGdvdDAynu3aC/Lp0s60joPhd3hh60w== X-Received: by 2002:a05:6a20:d04e:b0:1d5:1354:5256 with SMTP id adf61e73a8af0-1d989cf91c2mr6453359637.39.1729863116138; Fri, 25 Oct 2024 06:31:56 -0700 (PDT) Received: from localhost.localdomain ([240f:74:7be:1:9b52:57a4:8bdb:99d3]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921983sm1077883b3a.10.2024.10.25.06.31.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2024 06:31:55 -0700 (PDT) From: Koichiro Den To: kernel-team@lists.ubuntu.com Subject: [SRU][F][PATCH 2/3] ftrace: Return the first found result in lookup_rec() Date: Fri, 25 Oct 2024 22:31:05 +0900 Message-ID: <20241025133110.2839066-3-koichiro.den@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241025133110.2839066-1-koichiro.den@canonical.com> References: <20241025133110.2839066-1-koichiro.den@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Artem Savkov It appears that ip ranges can overlap so. In that case lookup_rec() returns whatever results it got last even if it found nothing in last searched page. This breaks an obscure livepatch late module patching usecase: - load livepatch - load the patched module - unload livepatch - try to load livepatch again To fix this return from lookup_rec() as soon as it found the record containing searched-for ip. This used to be this way prior lookup_rec() introduction. Link: http://lkml.kernel.org/r/20200306174317.21699-1-asavkov@redhat.com Cc: stable@vger.kernel.org Fixes: 7e16f581a817 ("ftrace: Separate out functionality from ftrace_location_range()") Signed-off-by: Artem Savkov Signed-off-by: Steven Rostedt (VMware) (cherry picked from commit d9815bff6b379ff46981bea9dfeb146081eab314) [koichiroden: Follow-up fix for the preceding prerequisite commit] CVE-2024-38588 Signed-off-by: Koichiro Den --- kernel/trace/ftrace.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index d9992fef7287..e40bbd18bc99 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -1552,6 +1552,8 @@ static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end) rec = bsearch(&key, pg->records, pg->index, sizeof(struct dyn_ftrace), ftrace_cmp_recs); + if (rec) + break; } return rec; } From patchwork Fri Oct 25 13:31:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Koichiro Den X-Patchwork-Id: 2002240 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XZkGm3FBRz1xtp for ; Sat, 26 Oct 2024 00:32:16 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1t4KQC-000745-H3; Fri, 25 Oct 2024 13:32:08 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1t4KQ9-0006zi-Jq for kernel-team@lists.ubuntu.com; Fri, 25 Oct 2024 13:32:05 +0000 Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id E52F640CE7 for ; Fri, 25 Oct 2024 13:32:01 +0000 (UTC) Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-71e67b6eed7so2593641b3a.0 for ; Fri, 25 Oct 2024 06:32:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729863118; x=1730467918; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1YAC0Tb0aD4GbLjIVF0ITfJxDA9IeIMIkg7TBbofBA4=; b=TJjnTQprgP5b2E+meNMryf/NB1ePzi1IkqD//yj4TCUn/Urf4qwVzfyNcklwqAx7SJ Tr7odYMNSIlWPd4B/Iv05vDyfNZxezvyOBHm0vD/PbdXjglFTiGyN+RQ8Dufr+mrrjKf PPgAAds2ePyYqk7XoIPySakTAwdtJqt+No7bjclGvA5gShtxcHuVIuspRovvUuGtQIcz NlF9QFUhTmWO5fFz87k1OMX4/TXGmIIS5NY4LEOIGVm4lk6Ibj6tXqg95vbakDmHE81F QCLGYT+rNJOSXTvjYGwZYqJVuTTA78sZFYJqhNkEGgWuQPeO3g9uT3+BZLYHk6cTgOO/ o7XQ== X-Gm-Message-State: AOJu0YyPA0UK4qvXrXrKp1mICNeQI6/akCrZ2UUEn93E3HU+RLoPntxO q4tQkgXv+/umVc6XvlU+1kh+Zvm7vjGAMyE+KAuc6HB2cjv1qrWNFzRJ4tB7SjdoV8C3U3K0/Zg 5YqrxA9DOjoebt6BhAsP451+rMBg21igmENnzuotqRRRg/mavSxS/Qn74joXApttuwapUZWQi1n 5QnnPs53yrFg== X-Received: by 2002:a05:6a21:114f:b0:1d9:3957:9af2 with SMTP id adf61e73a8af0-1d989a992dfmr7407468637.17.1729863117940; Fri, 25 Oct 2024 06:31:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGYUCrYKWqjOdhM2LtRWjo0Pu859wtmJm3bAvdzx6Sl4QwjrdEWHmVl3pSI6ol21i2k1ek09Q== X-Received: by 2002:a05:6a21:114f:b0:1d9:3957:9af2 with SMTP id adf61e73a8af0-1d989a992dfmr7407433637.17.1729863117600; Fri, 25 Oct 2024 06:31:57 -0700 (PDT) Received: from localhost.localdomain ([240f:74:7be:1:9b52:57a4:8bdb:99d3]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921983sm1077883b3a.10.2024.10.25.06.31.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2024 06:31:57 -0700 (PDT) From: Koichiro Den To: kernel-team@lists.ubuntu.com Subject: [SRU][F][PATCH 3/3] ftrace: Fix possible use-after-free issue in ftrace_location() Date: Fri, 25 Oct 2024 22:31:06 +0900 Message-ID: <20241025133110.2839066-4-koichiro.den@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241025133110.2839066-1-koichiro.den@canonical.com> References: <20241025133110.2839066-1-koichiro.den@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Zheng Yejian [ Upstream commit e60b613df8b6253def41215402f72986fee3fc8d ] KASAN reports a bug: BUG: KASAN: use-after-free in ftrace_location+0x90/0x120 Read of size 8 at addr ffff888141d40010 by task insmod/424 CPU: 8 PID: 424 Comm: insmod Tainted: G W 6.9.0-rc2+ [...] Call Trace: dump_stack_lvl+0x68/0xa0 print_report+0xcf/0x610 kasan_report+0xb5/0xe0 ftrace_location+0x90/0x120 register_kprobe+0x14b/0xa40 kprobe_init+0x2d/0xff0 [kprobe_example] do_one_initcall+0x8f/0x2d0 do_init_module+0x13a/0x3c0 load_module+0x3082/0x33d0 init_module_from_file+0xd2/0x130 __x64_sys_finit_module+0x306/0x440 do_syscall_64+0x68/0x140 entry_SYSCALL_64_after_hwframe+0x71/0x79 The root cause is that, in lookup_rec(), ftrace record of some address is being searched in ftrace pages of some module, but those ftrace pages at the same time is being freed in ftrace_release_mod() as the corresponding module is being deleted: CPU1 | CPU2 register_kprobes() { | delete_module() { check_kprobe_address_safe() { | arch_check_ftrace_location() { | ftrace_location() { | lookup_rec() // USE! | ftrace_release_mod() // Free! To fix this issue: 1. Hold rcu lock as accessing ftrace pages in ftrace_location_range(); 2. Use ftrace_location_range() instead of lookup_rec() in ftrace_location(); 3. Call synchronize_rcu() before freeing any ftrace pages both in ftrace_process_locs()/ftrace_release_mod()/ftrace_free_mem(). Link: https://lore.kernel.org/linux-trace-kernel/20240509192859.1273558-1-zhengyejian1@huawei.com Cc: stable@vger.kernel.org Cc: Cc: Cc: Fixes: ae6aa16fdc16 ("kprobes: introduce ftrace based optimization") Suggested-by: Steven Rostedt Signed-off-by: Zheng Yejian Signed-off-by: Steven Rostedt (Google) Signed-off-by: Sasha Levin (backported from commit 8ea8ef5e42173560ac510e92a1cc797ffeea8831 linux-5.15.y) [koichiroden: Dropped changes on ftrace_location() due to missing commit aebfd12521d9 ("x86/ibt,ftrace: Search for __fentry__ location"). That means "To fix this issue > 2." in the original commit message is unnecessary. Notice that Intel IBT support is not backported] CVE-2024-38588 Signed-off-by: Koichiro Den --- kernel/trace/ftrace.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index e40bbd18bc99..5e4f5a9976c2 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -1573,12 +1573,15 @@ static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end) unsigned long ftrace_location_range(unsigned long start, unsigned long end) { struct dyn_ftrace *rec; + unsigned long ip = 0; + rcu_read_lock(); rec = lookup_rec(start, end); if (rec) - return rec->ip; + ip = rec->ip; + rcu_read_unlock(); - return 0; + return ip; } /** @@ -5746,6 +5749,8 @@ static int ftrace_process_locs(struct module *mod, /* We should have used all pages unless we skipped some */ if (pg_unuse) { WARN_ON(!skipped); + /* Need to synchronize with ftrace_location_range() */ + synchronize_rcu(); ftrace_free_pages(pg_unuse); } return ret; @@ -5899,6 +5904,9 @@ void ftrace_release_mod(struct module *mod) out_unlock: mutex_unlock(&ftrace_lock); + /* Need to synchronize with ftrace_location_range() */ + if (tmp_page) + synchronize_rcu(); for (pg = tmp_page; pg; pg = tmp_page) { /* Needs to be called outside of ftrace_lock */ @@ -6206,6 +6214,7 @@ void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr) unsigned long start = (unsigned long)(start_ptr); unsigned long end = (unsigned long)(end_ptr); struct ftrace_page **last_pg = &ftrace_pages_start; + struct ftrace_page *tmp_page = NULL; struct ftrace_page *pg; struct dyn_ftrace *rec; struct dyn_ftrace key; @@ -6249,12 +6258,8 @@ void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr) ftrace_update_tot_cnt--; if (!pg->index) { *last_pg = pg->next; - if (pg->records) { - free_pages((unsigned long)pg->records, pg->order); - ftrace_number_of_pages -= 1 << pg->order; - } - ftrace_number_of_groups--; - kfree(pg); + pg->next = tmp_page; + tmp_page = pg; pg = container_of(last_pg, struct ftrace_page, next); if (!(*last_pg)) ftrace_pages = pg; @@ -6271,6 +6276,11 @@ void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr) clear_func_from_hashes(func); kfree(func); } + /* Need to synchronize with ftrace_location_range() */ + if (tmp_page) { + synchronize_rcu(); + ftrace_free_pages(tmp_page); + } } void __init ftrace_free_init_mem(void)