Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp916954pxk; Thu, 10 Sep 2020 01:58:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzyDIf6JBN+eVQZzn6x8vFrPv7J2GwlpO3hBVU/v4nsg0wOryOAGihEC5LMRFh45DGCIS1l X-Received: by 2002:aa7:c504:: with SMTP id o4mr8395814edq.82.1599728290404; Thu, 10 Sep 2020 01:58:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599728290; cv=none; d=google.com; s=arc-20160816; b=f1xIW/8e+BWQrcBML6mA349x61gwXsvYF9yiheEG1JDHWaJLlRmyh7XXIQuLFExnZF PPOh0+uccKMybKCNLt8Qg2qEsFCIRqBSnBNROhx3poVkeXI4Va1SfOJD/Au8/HeTz5Cj H0W4Ptzpi3a6GuNUvp5TvYrXICP0+e5c3WgDzED1lsXAkmakc9ATaeWJUTRqGPqpusy9 eCjyxUYEvzXnvy3Ey5ParQVPX02TagbmZJO8485cqxy77qL4s8ZH5Oemi0fMNBNpQlil pOeCSbrFm3kQfsHDoO1n2XX9VkTEuLIDZvSeQBP2BjA5GnP4/Ninu3qtQNciVmN2eLhC 43gQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=DTaPRxp7Z8lpMoMAhYpo2Kx8EXUNYeD1bteZLgr8dFM=; b=Nhx38KQRiYgr5d7/HR5jFu89o7HqmWkUHh6h2A38ZB7DOM0d66SQ5eMZRVkID+Aj7J Wkna5KFyh+fEQQdJyJ+hU10BIqtM7mrnsvBvgu0zU3yUqStPwv0Yyz7CpDZTpt0TfFQV jp2kx0qhhCIYJzxwAv9q85mLRD2ED3zBbiA+AiPaZqeK2kODQNJoD41pFieKMvo7EiXJ lWDpjsn8Gs3L9buye+9i5XkEb5KFz4epez/gIlPUcB3wNhD8wrvc+AVHY0QjbEu9c6V2 wKADpnmAQ/e7wzj7g153dwrp35lkuPhG4y8JbVwA7RJw16J0Ii5lMFEvJWcywUEtc1YZ r6vg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=sWdRgGJz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r26si3189804eda.569.2020.09.10.01.57.47; Thu, 10 Sep 2020 01:58:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=sWdRgGJz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730466AbgIJI4k (ORCPT + 99 others); Thu, 10 Sep 2020 04:56:40 -0400 Received: from mail.kernel.org ([198.145.29.99]:36878 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730367AbgIJIzO (ORCPT ); Thu, 10 Sep 2020 04:55:14 -0400 Received: from localhost.localdomain (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E9AC820C09; Thu, 10 Sep 2020 08:55:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1599728109; bh=rfxlWXkoFVg1nBhyifXGxOJ6QA+J4LqvbC5/ABiyWGI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sWdRgGJzJ/iOoEw77p0uCQzWo6ihi5UVTZdPvCAp633+7sgdfbtl0/e3ou+I885Qp 1U3uSuCPIbtBHNpFdjTtrMnwk3OaTmx4SA0Vcmt5Pe2dDpeDkIwYUbGmS8vPhWpIGF 7fqmwQLWsYawtSEx+xgpVlSpA87TMH6T4dsihthM= From: Masami Hiramatsu To: Steven Rostedt , Jonathan Corbet , Shuah Khan Cc: mhiramat@kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Randy Dunlap , Ingo Molnar , linux-kselftest@vger.kernel.org Subject: [PATCH v3 1/8] kprobes: tracing/kprobes: Fix to kill kprobes on initmem after boot Date: Thu, 10 Sep 2020 17:55:05 +0900 Message-Id: <159972810544.428528.1839307531600646955.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <159972809455.428528.4737752126800169128.stgit@devnote2> References: <159972809455.428528.4737752126800169128.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Since kprobe_event= cmdline option allows user to put kprobes on the functions in initmem, kprobe has to make such probes gone after boot. Currently the probes on the init functions in modules will be handled by module callback, but the kernel init text isn't handled. Without this, kprobes may access non-exist text area to disable or remove it. Fixes: 970988e19eb0 ("tracing/kprobe: Add kprobe_event= boot parameter") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu --- Changes in v2: - Fix kprobe_free_init_mem() not depending on CONFIG_DEBUG_FS. --- include/linux/kprobes.h | 5 +++++ init/main.c | 2 ++ kernel/kprobes.c | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/include/linux/kprobes.h b/include/linux/kprobes.h index 9be1bff4f586..8aab327b5539 100644 --- a/include/linux/kprobes.h +++ b/include/linux/kprobes.h @@ -373,6 +373,8 @@ void unregister_kretprobes(struct kretprobe **rps, int num); void kprobe_flush_task(struct task_struct *tk); void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head); +void kprobe_free_init_mem(void); + int disable_kprobe(struct kprobe *kp); int enable_kprobe(struct kprobe *kp); @@ -435,6 +437,9 @@ static inline void unregister_kretprobes(struct kretprobe **rps, int num) static inline void kprobe_flush_task(struct task_struct *tk) { } +static inline void kprobe_free_init_mem(void) +{ +} static inline int disable_kprobe(struct kprobe *kp) { return -ENOSYS; diff --git a/init/main.c b/init/main.c index ae78fb68d231..038128b2a755 100644 --- a/init/main.c +++ b/init/main.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -1402,6 +1403,7 @@ static int __ref kernel_init(void *unused) kernel_init_freeable(); /* need to finish all async __init code before freeing the memory */ async_synchronize_full(); + kprobe_free_init_mem(); ftrace_free_init_mem(); free_initmem(); mark_readonly(); diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 287b263c9cb9..2880cdf37c47 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -2452,6 +2452,28 @@ static struct notifier_block kprobe_module_nb = { extern unsigned long __start_kprobe_blacklist[]; extern unsigned long __stop_kprobe_blacklist[]; +void kprobe_free_init_mem(void) +{ + void *start = (void *)(&__init_begin); + void *end = (void *)(&__init_end); + struct hlist_head *head; + struct kprobe *p; + int i; + + mutex_lock(&kprobe_mutex); + + /* Kill all kprobes on initmem */ + for (i = 0; i < KPROBE_TABLE_SIZE; i++) { + head = &kprobe_table[i]; + hlist_for_each_entry(p, head, hlist) { + if (start <= (void *)p->addr && (void *)p->addr < end) + kill_kprobe(p); + } + } + + mutex_unlock(&kprobe_mutex); +} + static int __init init_kprobes(void) { int i, err = 0;