From patchwork Wed Mar 24 17:40:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12161939 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.0 required=3.0 tests=BAYES_00,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CFD3EC433DB for ; Wed, 24 Mar 2021 17:41:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7D32F61A1B for ; Wed, 24 Mar 2021 17:41:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237126AbhCXRlB convert rfc822-to-8bit (ORCPT ); Wed, 24 Mar 2021 13:41:01 -0400 Received: from us-smtp-delivery-44.mimecast.com ([205.139.111.44]:32900 "EHLO us-smtp-delivery-44.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237209AbhCXRkk (ORCPT ); Wed, 24 Mar 2021 13:40:40 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-322-BiaTXAiaOfG_77Pga_j6EA-1; Wed, 24 Mar 2021 13:40:35 -0400 X-MC-Unique: BiaTXAiaOfG_77Pga_j6EA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1BC40881283; Wed, 24 Mar 2021 17:40:34 +0000 (UTC) Received: from krava.redhat.com (unknown [10.40.196.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 677145D9D0; Wed, 24 Mar 2021 17:40:31 +0000 (UTC) From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt Subject: [PATCHv2] bpf: Take module reference for trampoline in module Date: Wed, 24 Mar 2021 18:40:30 +0100 Message-Id: <20210324174030.2053353-1-jolsa@kernel.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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 --- v2 changes: - fixed ip_module_put to do preempt_disable/preempt_enable kernel/bpf/trampoline.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index 1f3a4be4b175..39e4280f94e4 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -87,6 +87,26 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key) return tr; } +static struct module *ip_module_get(unsigned long ip) +{ + struct module *mod; + int err = 0; + + preempt_disable(); + mod = __module_text_address(ip); + if (mod && !try_module_get(mod)) + err = -ENOENT; + preempt_enable(); + return err ? ERR_PTR(err) : mod; +} + +static void ip_module_put(unsigned long ip) +{ + preempt_disable(); + module_put(__module_text_address(ip)); + preempt_enable(); +} + static int is_ftrace_location(void *ip) { long addr; @@ -108,6 +128,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) + ip_module_put((unsigned long) ip); return ret; } @@ -126,6 +149,7 @@ static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_ad /* first time registering */ static int register_fentry(struct bpf_trampoline *tr, void *new_addr) { + struct module *mod; void *ip = tr->func.addr; int ret; @@ -134,10 +158,17 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr) return ret; tr->func.ftrace_managed = ret; + mod = ip_module_get((unsigned long) ip); + if (IS_ERR(mod)) + 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) + module_put(mod); return ret; }