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)