Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp779331ybj; Tue, 5 May 2020 07:26:40 -0700 (PDT) X-Google-Smtp-Source: APiQypJCv5GtWs4Wa8xCnSi0MYW35eYiSZkSVKiUJ1/V1ZzDzqBHa+8gP2fH83fvaLB6ZOJNC5GO X-Received: by 2002:a05:6402:1768:: with SMTP id da8mr2914330edb.216.1588688800349; Tue, 05 May 2020 07:26:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588688800; cv=none; d=google.com; s=arc-20160816; b=jrzvvNlPv6d911Dp/T4bXk0v86hXnZMH4ZgqlSnTnbWEeHQ8RBTeX1TT6GmvfBlsci DQ+uXMcsT5agkJ2cpaoeNJDfqktrb5lTPoeX23rZnVmSoGpK2DkoVNGBxVD6yHJj7wWS p0cX+neZeXHM/iCOL0mDumPdQJ4dt2pTWqxe/S5PJgk0WQjpscKIb7HnMGEIi8GwaWVb bpCjGrdsBWmnl4S9cvzbgKjyVowegJgEa7ih9q34WSsgmikycQtSvGqIVQSsL13OvJyE wXW+bfVMIsB3ZJSMJW3aDvBkeROMbr0u7AIeqpOSx6GipQvDZ3R33RN/60FTuP9TUF7z Xolg== 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 :references:subject:cc:to:from:date:user-agent:message-id; bh=g76YD2MEnQR3yajfxlR5F5rT/vezG1J7wPZhnSYIap8=; b=xm9kRh3kmytAXXgU178X2f/ZjRA9M37R8Uk09UYTsZPhuWw9c4fht1Z5/U0bd8VF8Z GfVMPsbekkJIbjiHCSv5sc+MDMX3gflt+5L9WRFsjz2f7cdJw4mTU0iHz7VfM5tR/0Bl tQRvfMVNqYjJ29VVx5Z5jNsKooA0Wo8W2yegnIfcVJG7y54GXJFD2wJVu4Y+lVdxhvJL bumwVWzrptLOq9SL+X7C/tCiYLiIC7JQMaAGmmv0f0l5bqS4PuNWbLUJw7H9nGmlaZTV mw5d6IssqAPNYVoY35nomYL02m6wVnd62BKOlKOZOl/xop6S6j1Y8fxozQ5XpMMgkEoT XRwA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o25si1343610ejh.73.2020.05.05.07.26.16; Tue, 05 May 2020 07:26:40 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729870AbgEEOX6 (ORCPT + 99 others); Tue, 5 May 2020 10:23:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1729434AbgEEONw (ORCPT ); Tue, 5 May 2020 10:13:52 -0400 Received: from Galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23754C061A10 for ; Tue, 5 May 2020 07:13:52 -0700 (PDT) Received: from p5de0bf0b.dip0.t-ipconnect.de ([93.224.191.11] helo=nanos.tec.linutronix.de) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1jVyKE-0000Uv-7f; Tue, 05 May 2020 16:13:34 +0200 Received: from nanos.tec.linutronix.de (localhost [IPv6:::1]) by nanos.tec.linutronix.de (Postfix) with ESMTP id B61D5FFC8D; Tue, 5 May 2020 16:13:33 +0200 (CEST) Message-Id: <20200505134059.678201813@linutronix.de> User-Agent: quilt/0.65 Date: Tue, 05 May 2020 15:16:18 +0200 From: Thomas Gleixner To: LKML Cc: x86@kernel.org, "Paul E. McKenney" , Andy Lutomirski , Alexandre Chartre , Frederic Weisbecker , Paolo Bonzini , Sean Christopherson , Masami Hiramatsu , Petr Mladek , Steven Rostedt , Joel Fernandes , Boris Ostrovsky , Juergen Gross , Brian Gerst , Mathieu Desnoyers , Josh Poimboeuf , Will Deacon Subject: [patch V4 part 1 16/36] kprobes: Support __kprobes blacklist in modules References: <20200505131602.633487962@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-transfer-encoding: 8-bit X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masami Hiramatsu Support __kprobes attribute for blacklist functions in modules. The __kprobes attribute functions are stored in .kprobes.text section. Signed-off-by: Masami Hiramatsu Signed-off-by: Thomas Gleixner Link: https://lkml.kernel.org/r/158523418821.24735.15379873028352411526.stgit@devnote2 --- include/linux/module.h | 4 ++++ kernel/kprobes.c | 42 ++++++++++++++++++++++++++++++++++++++++++ kernel/module.c | 4 ++++ 3 files changed, 50 insertions(+) --- a/include/linux/module.h +++ b/include/linux/module.h @@ -489,6 +489,10 @@ struct module { unsigned int num_ftrace_callsites; unsigned long *ftrace_callsites; #endif +#ifdef CONFIG_KPROBES + void *kprobes_text_start; + unsigned int kprobes_text_size; +#endif #ifdef CONFIG_LIVEPATCH bool klp; /* Is this a livepatch module? */ --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -2179,6 +2179,19 @@ int kprobe_add_area_blacklist(unsigned l return 0; } +/* Remove all symbols in given area from kprobe blacklist */ +static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end) +{ + struct kprobe_blacklist_entry *ent, *n; + + list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) { + if (ent->start_addr < start || ent->start_addr >= end) + continue; + list_del(&ent->list); + kfree(ent); + } +} + int __init __weak arch_populate_kprobe_blacklist(void) { return 0; @@ -2215,6 +2228,28 @@ static int __init populate_kprobe_blackl return ret ? : arch_populate_kprobe_blacklist(); } +static void add_module_kprobe_blacklist(struct module *mod) +{ + unsigned long start, end; + + start = (unsigned long)mod->kprobes_text_start; + if (start) { + end = start + mod->kprobes_text_size; + kprobe_add_area_blacklist(start, end); + } +} + +static void remove_module_kprobe_blacklist(struct module *mod) +{ + unsigned long start, end; + + start = (unsigned long)mod->kprobes_text_start; + if (start) { + end = start + mod->kprobes_text_size; + kprobe_remove_area_blacklist(start, end); + } +} + /* Module notifier call back, checking kprobes on the module */ static int kprobes_module_callback(struct notifier_block *nb, unsigned long val, void *data) @@ -2225,6 +2260,11 @@ static int kprobes_module_callback(struc unsigned int i; int checkcore = (val == MODULE_STATE_GOING); + if (val == MODULE_STATE_COMING) { + mutex_lock(&kprobe_mutex); + add_module_kprobe_blacklist(mod); + mutex_unlock(&kprobe_mutex); + } if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE) return NOTIFY_DONE; @@ -2255,6 +2295,8 @@ static int kprobes_module_callback(struc kill_kprobe(p); } } + if (val == MODULE_STATE_GOING) + remove_module_kprobe_blacklist(mod); mutex_unlock(&kprobe_mutex); return NOTIFY_DONE; } --- a/kernel/module.c +++ b/kernel/module.c @@ -3194,6 +3194,10 @@ static int find_module_sections(struct m sizeof(*mod->ei_funcs), &mod->num_ei_funcs); #endif +#ifdef CONFIG_KPROBES + mod->kprobes_text_start = section_objs(info, ".kprobes.text", 1, + &mod->kprobes_text_size); +#endif mod->extable = section_objs(info, "__ex_table", sizeof(*mod->extable), &mod->num_exentries);