linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Jiri Olsa <jolsa@kernel.org>, Alexei Starovoitov <ast@kernel.org>,
	Sasha Levin <sashal@kernel.org>,
	netdev@vger.kernel.org, bpf@vger.kernel.org
Subject: [PATCH AUTOSEL 5.11 17/51] bpf: Take module reference for trampoline in module
Date: Mon, 12 Apr 2021 12:22:22 -0400	[thread overview]
Message-ID: <20210412162256.313524-17-sashal@kernel.org> (raw)
In-Reply-To: <20210412162256.313524-1-sashal@kernel.org>

From: Jiri Olsa <jolsa@kernel.org>

[ Upstream commit 861de02e5f3f2a104eecc5af1d248cb7bf8c5f75 ]

Currently module can be unloaded even if there's a trampoline
register in it. It's easily reproduced by running in parallel:

  # while :; do ./test_progs -t module_attach; done
  # while :; do rmmod bpf_testmod; sleep 0.5; done

Taking the module reference in case the trampoline's ip is
within the module code. Releasing it when the trampoline's
ip is unregistered.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210326105900.151466-1-jolsa@kernel.org
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 include/linux/bpf.h     |  2 ++
 kernel/bpf/trampoline.c | 30 ++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 564ebf91793e..88b581b75d5b 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -41,6 +41,7 @@ struct bpf_local_storage;
 struct bpf_local_storage_map;
 struct kobject;
 struct mem_cgroup;
+struct module;
 
 extern struct idr btf_idr;
 extern spinlock_t btf_idr_lock;
@@ -630,6 +631,7 @@ struct bpf_trampoline {
 	/* Executable image of trampoline */
 	struct bpf_tramp_image *cur_image;
 	u64 selector;
+	struct module *mod;
 };
 
 struct bpf_attach_target_info {
diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index 986dabc3d11f..a431d7af884c 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -9,6 +9,7 @@
 #include <linux/btf.h>
 #include <linux/rcupdate_trace.h>
 #include <linux/rcupdate_wait.h>
+#include <linux/module.h>
 
 /* dummy _ops. The verifier will operate on target program's ops. */
 const struct bpf_verifier_ops bpf_extension_verifier_ops = {
@@ -87,6 +88,26 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
 	return tr;
 }
 
+static int bpf_trampoline_module_get(struct bpf_trampoline *tr)
+{
+	struct module *mod;
+	int err = 0;
+
+	preempt_disable();
+	mod = __module_text_address((unsigned long) tr->func.addr);
+	if (mod && !try_module_get(mod))
+		err = -ENOENT;
+	preempt_enable();
+	tr->mod = mod;
+	return err;
+}
+
+static void bpf_trampoline_module_put(struct bpf_trampoline *tr)
+{
+	module_put(tr->mod);
+	tr->mod = NULL;
+}
+
 static int is_ftrace_location(void *ip)
 {
 	long addr;
@@ -108,6 +129,9 @@ static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
 		ret = unregister_ftrace_direct((long)ip, (long)old_addr);
 	else
 		ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
+
+	if (!ret)
+		bpf_trampoline_module_put(tr);
 	return ret;
 }
 
@@ -134,10 +158,16 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
 		return ret;
 	tr->func.ftrace_managed = ret;
 
+	if (bpf_trampoline_module_get(tr))
+		return -ENOENT;
+
 	if (tr->func.ftrace_managed)
 		ret = register_ftrace_direct((long)ip, (long)new_addr);
 	else
 		ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
+
+	if (ret)
+		bpf_trampoline_module_put(tr);
 	return ret;
 }
 
-- 
2.30.2


  parent reply	other threads:[~2021-04-12 16:24 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-12 16:22 [PATCH AUTOSEL 5.11 01/51] net: ieee802154: fix nl802154 del llsec key Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 02/51] net: ieee802154: fix nl802154 del llsec dev Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 03/51] net: ieee802154: fix nl802154 add llsec key Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 04/51] net: ieee802154: fix nl802154 del llsec devkey Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 05/51] net: ieee802154: nl-mac: fix check on panid Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 06/51] ARM: dts: Drop duplicate sha2md5_fck to fix clk_disable race Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 07/51] ARM: dts: Fix moving mmc devices with aliases for omap4 & 5 Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 08/51] remoteproc: pru: Fix loading of GNU Binutils ELF Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 09/51] lockdep: Add a missing initialization hint to the "INFO: Trying to register non-static key" message Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 10/51] arc: kernel: Return -EFAULT if copy_to_user() fails Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 11/51] iwlwifi: Fix softirq/hardirq disabling in iwl_pcie_enqueue_hcmd() Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 12/51] xfrm: BEET mode doesn't support fragments for inner packets Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 13/51] ASoC: max98373: Changed amp shutdown register as volatile Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 14/51] ASoC: max98373: Added 30ms turn on/off time delay Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 15/51] net: axienet: allow setups without MDIO Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 16/51] gpu/xen: Fix a use after free in xen_drm_drv_init Sasha Levin
2021-04-12 16:22 ` Sasha Levin [this message]
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 18/51] neighbour: Disregard DEAD dst in neigh_update Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 19/51] powerpc/signal32: Fix Oops on sigreturn with unmapped VDSO Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 20/51] ARM: keystone: fix integer overflow warning Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 21/51] ARM: omap1: fix building with clang IAS Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 22/51] drivers: net: fix memory leak in atusb_probe Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 23/51] drivers: net: fix memory leak in peak_usb_create_dev Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 24/51] drm/msm: Fix a5xx/a6xx timestamps Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 25/51] ASoC: fsl_esai: Fix TDM slot setup for I2S mode Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 26/51] scsi: scsi_transport_srp: Don't block target in SRP_PORT_LOST state Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 27/51] iwlwifi: add support for Qu with AX201 device Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 28/51] net: ieee802154: forbid monitor for set llsec params Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 29/51] net: ieee802154: stop dump llsec keys for monitors Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 30/51] net: ieee802154: forbid monitor for add llsec key Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 31/51] net: ieee802154: forbid monitor for del " Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 32/51] net: ieee802154: stop dump llsec devs for monitors Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 33/51] net: ieee802154: forbid monitor for add llsec dev Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 34/51] net: ieee802154: forbid monitor for del " Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 35/51] net: ieee802154: stop dump llsec devkeys for monitors Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 36/51] net: ieee802154: forbid monitor for add llsec devkey Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 37/51] net: ieee802154: forbid monitor for del " Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 38/51] net: ieee802154: stop dump llsec seclevels for monitors Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 39/51] net: ieee802154: forbid monitor for add llsec seclevel Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 40/51] net: ieee802154: forbid monitor for del " Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 41/51] net: ieee802154: stop dump llsec params for monitors Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 42/51] net: mac802154: Fix general protection fault Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 43/51] pcnet32: Use pci_resource_len to validate PCI resource Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 44/51] net: tun: set tun->dev->addr_len during TUNSETLINK processing Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 45/51] net/rds: Avoid potential use after free in rds_send_remove_from_sock Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 46/51] net: tipc: Fix spelling errors in net/tipc module Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 47/51] drm/amd/display: Add missing mask for DCN3 Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 48/51] mac80211: clear sta->fast_rx when STA removed from 4-addr VLAN Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 49/51] cfg80211: remove WARN_ON() in cfg80211_sme_connect Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 50/51] virt_wifi: Return micros for BSS TSF values Sasha Levin
2021-04-12 16:22 ` [PATCH AUTOSEL 5.11 51/51] lib: fix kconfig dependency on ARCH_WANT_FRAME_POINTERS Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210412162256.313524-17-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).